BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Node 7.6 Brings Default Async/Await Support

Node 7.6 Brings Default Async/Await Support

Leia em Português

This item in japanese

Bookmarks

Node.js 7.6 has shipped with official support for async/await enabled by default and better performance on low-memory devices.

Async/await support in Node 7.6 comes from updating V8, Chromium’s JavaScript engine, to version 5.5. This means async/await is not considered experimental anymore and can be used without specifying the --harmony flag, which can be used to enable almost-completed features that the V8 team does not consider stable yet.

The main benefit of async/await is avoiding callback hell, which ensues from nesting a sequence of asynchronous operation through their respective callbacks.

This is how, for example, you could handle a sequence of two asynchronous operations using callbacks:

function asyncOperation(callback) {
  asyncStep1(function(response1) {
    asyncStep2(response1, function(response2) {
        callback(...);
    });
  });
}

The use of async/await allows to streamline that code and make it appear as if it were a sequence of synchronous operations:

async function asyncOperation(callback) {
    const response = await asyncStep1();
    return await asyncStep2(response);
}

Another approach to solving callback hell is using Promises, a feature that has been long available in JavaScript. With Promises, the above example would become:

function asyncOperation() {
  return asyncStep1(...)
    .then(asyncStep2(...));
}

Still, using Promises can become cumbersome in more complex situations.

V8 5.5 also introduces several improvements to heap size and zone usage that reduce its memory footprint up to 35% on low-memory devices compared to V8 5.3.

Other notable changes in Node 7.6 are:

  • The old CLI debugger node debug has been rewritten against the new debugger protocol node --inspect, which will be the only one supported in future versions of V8.
  • Support for the file: protocol has been added to fs, so you can write fs.readFile(URL('file:///C:/path/to/file');, (err, data) => {});

In addition to V8 5.5, Node 7.6 also includes other upgraded dependencies, such as cross-platform async I/O library libuv 1.11, and zlib 1.2.11.

Rate this Article

Adoption
Style

BT