Promises in Node.js 8.x Core

styfle
Frontend Weekly
Published in
2 min readMay 10, 2017

Promises are well-known in the JavaScript community since they landed in Chrome 32 (Jan 2014) and in Node.js 0.12 (Feb 2015). They were formally introduced as part of the ES6 (ES2015) spec and are even more important in 2017 due to the async/await primitives that landed in Chrome 55 (Dec 2016) and Node.js 7 (Feb 2017).

Ok so you already knew all that history because you’re a young, hip, state-of-the-art, JavaScript developer who writes hand-crafted, artisan-made code aged to perfection. Big whoop, wanna fight about it?

But did you know that Node.js 8 will have a Promise API shipped in core? No way? Too good to be true? Well yes, I may have fibbed a bit.

Node.js 8 will have a utility to “promisify” any core API. This is just as good and will keep the Node.js core small.

How does it work?

I’ll give you two easy steps: Require and Wrap.

  1. Require const promisify = require('util').promisify;
  2. Wrap const stat = promisify(require('fs').stat);

Ok so now you’re saying this is exactly what bluebird does because you’re a savvy, hipster code-monkey who started using Promises before they were cool.

You would be correct except that you’re missing how important this is! This means user-land dependencies are no longer needed to write async/await! Hurray! See the following code example.

Get On With It!

Here is is a simple example that updates an existing file or creates the file if it does not exist.

Some say this is callback hell, but we only went a couple layers deep so it’s really more like callback purgatory. Now let’s see how it looks with promisify!

This code is asynchronous but there are no callbacks! The use if/else and try/catch is pretty intuitive and looks like synchronous code.

Now there is a little bit of boilerplate in order to promisify each function. So how about a helper function?

Not too shabby! Now it’s your turn to see what kind of async code you can cook up with Node.js 8!

--

--

styfle
Frontend Weekly

Software Engineer. Follow my blog https://styfle.dev or follow me on twitter @styfle