22Sep2015

Application Development: Asynchronous JavaScript code by Promises

Both front-end JavaScript code and runned on server side node.js applications are designed not just a long-run set of instructions, but rather a set of functions, which are runned by the engine as handlers of appropriate events. E.g., user clicks the button, server received request from the client, page loaded, server started started. Any JavaScript engine is just very sophisticated implementation of Event Loop pattern. There is a lot of stuff written about V8 engine, which node.js based on. Such architecture has asynchronous nature by design. That’s why it’s very important to deal well with asynchronous Application Development for any professional JavaScript developer.

Callbacks and other approaches

The simplest native way to deal with asynchronous code is to use callbacks. It may be ok in case of small application or use case. On the other hand, in real life Application Development, Iflexion company have complicated huge solutions splitted into many modules and layers. To use pure callbacks in this situation is antipattern. The code is not readable. It’s too hard to maintain it. That’s why JavaScript community have several approaches to hold callbacks hell under control. E.g., Fiber is JavaScript abstraction similar to co-routine. Real threads are managed by the system. Fibers are controlled by user code. It’s a bit dangerous equation, because it forces us to provide bullet-proof code, but it works. There is a special fork of CoffeeScript (JavaScript preprocessor with ruby-like minimalist syntax) with await syntax construction to deal with asynchronous calls on the level of language. No matter what approach is applicable or not, the best way for JavaScript Application Development is Promises [https://www.promisejs.org].

 Promises

Promises is a technique to organize asynchronous code callbacks into the objects. That’s in its name. Asynchronous call returns us an object as a promise of further result or error callback. There are several libraries to implement Promises scheme. E.g., Q [http://documentup.com/kriskowal/q] and Bluebird [http://bluebirdjs.com/docs/getting-started.html]. Bluebird is much faster and better than Q, because Q has a lot of stuff at line of code security in the browser. Thus, use Bluebird both on the server side and as front-end asynchronous model.

 Approaches

Regular promise object has then method where we specify what action should be done on successful finish of the asynchronous call a promise keep an eye on. There may be another promise as well. As you see, we have a queue of promises called one by one asynchronously. Regular callback has special argument to indicate an error as a result of operation. Promises allows us to catch the error in any place of the queue by catch method. Usually, we put error handler at the end of the sequence of asynchronous calls. It’s easy to convert callbacks into promises. There is promisify process to generate promises version of regular callback-based methods.

Read also

Comments are closed.