Loading [Contrib]/a11y/accessibility-menu.js
Back
Close

What's new in Node 8.0.0

VonRickroll
5,284 views

Node 8.0.0 is here! Released on May 30th 2017, It enters Node's Long Term Support roadmap and it brings some new interesting features to the table. The complete release note is available on the official Node blog, but let's take a look at the some of the release highlights.


Promisify

APIs with a standard callback style, such as fs.readFile(path, callback) or request(path, callback) can now be wrapped into promises by using util.promisify(). It creates a function that returns a promise.

Promisify
const fs = require('fs');
const util = require('util');
// This will create a "promisified" version of fs.readFile
const readfile = util.promisify(fs.readFile);
readfile('./file-to-read.txt') // This returns a promise
.then((data) => {
console.log(data.toString());
})
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Async Hooks

async_hooks is an experimental module that helps you monitor async operations. You can define hooks for the various stages of an async request's lifecycle. The constructor async_hooks.createHook takes an object as a parameter, which contains a few different callbacks:

// Create a new AsyncHook instance. All of these callbacks are optional.
const asyncHook = async_hooks.createHook({
  init: initCallback,
  before: beforeCallback,
  after: afterCallback,
  destroy: destroyCallback
});
Async Hooks
// Adapted from the official documentation at https://github.com/AndreasMadsen/node/blob/82149a8e0f0a13e5f34215e370b6750a300617e6/doc/api/async_hooks.md
const async_hooks = require('async_hooks');
const fs = require('fs');
const util = require('util');
// Some other useful APIs
// Return the ID of the current execution context.
const cid = async_hooks.currentId();
// Return the ID of the handle responsible for triggering the callback of the
// current execution scope to call.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Read the official documentation for async_hooks. Be aware that async_hooks is a work in progress and "users should take great care when using the experimental new module".


URL parser API

Node 8.0.0 now supports the WHATWG URL standard. This helps you parse and handle URL objects easily. There is an official documentation guide for the WHATWG URL methods, but here are some of them.

URL API
const URL = require('url').URL;
const myURL = new URL('https://example.org:8080/foo?first=john&last=doe#bar');
console.log(`## Properties:
href: ${myURL.href}
protocol: ${myURL.protocol}
pathname: ${myURL.pathname}
host: ${myURL.host}
port: ${myURL.port}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Read the official documentation for the WHATWG URL API.


--pending-deprecation

--pending-deprecation is a new flag that will warn you if your Node program is using features that are pending deprecation. For instance, in Node 8.0.0, the constructor Buffer(num) for the Buffer API (used to create 0-filled Buffer instances) is depreciating. If you continue to use it and launch Node with the --pending-deprecation flag, it will emit a DeprecationWarning warning.

The following snippets runs Node 8.0.0 with the --pending-deprecation flag. Try it out.

Deprecation Warnings
// Instanciating a Buffer with Buffer.alloc(num)
const buffer1 = Buffer.alloc(10); // Should run fine
// Instanciating a Buffer with the Buffer(num) constructor
const buffer2 = Buffer(10); // Should emit a DeprecationWarning warning
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

These are the key features in this release but Node 8.0.0 comes with other new features. Have fun checking them out and reading the official release note.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants