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 protocolnode --inspect
, which will be the only one supported in future versions of V8. - Support for the
file:
protocol has been added tofs
, so you can writefs.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.