1

I hear that Promises are available for Node core APIs. For example with fs, how can we use promises, do we just omit the callback?

fs.readFile(file).then(v => console.log(v)); 

or how do we use?

at the moment for Node.js versions older than 10, I am guessing, it's behind a flag? Maybe: node --promises?

3 Answers3

5

As of now, most Node callback-based APIs should be manually promisified in order to make use of promises.

util.promisify is available since Node 8 (also polyfillable) and allows to promisify any APIs that use error-first callbacks, including built-in fs:

const { promisify } = require('util');
const fs =  require('fs');
const readFile = promisify(fs.readFile);

readFile(file).then(console.log);

Some APIs that use non-error-first callbacks support it too via util.promisify.custom symbol, e.g. setTimeout:

promisify(setTimeout)(100).then(...);

fs.promises experimental API is available since Node 10 (polyfillable in Node 8 and higher) and contains promisified fs module, as already mentioned in another question:

const fsp = require('fs').promises;

fsp.readFile(file).then(console.log);

For batch promisification third-party tools like pify can be used.

Third-party promisification packages are available for most built-in and popular third-party callback-based packages that can be converted to promises, e.g. fs-extra, which is a preferable drop-in replacement for fs that includes essential graceful-fs package.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
3

This experimental feature was added in node v10.0.0. You'll need to require fs/promises instead of fs

Evert
  • 93,428
  • 18
  • 118
  • 189
  • Are you sure about this? The [node.js doc](https://nodejs.org/api/fs.html#fs_fs_promises_api) says: `require('fs').promises`. – jfriend00 Jun 30 '18 at 03:38
  • @jfriend00 Seems like it was fs/promises not so long ago. Yes, it's `require('fs').promises` now. They wouldn't label it as experimental for nothing. – Estus Flask Jun 30 '18 at 06:11
  • I looked into it a bit more and it appears that `fs/promises` is what the latest code shows. It was changed to that from `fs.promises`. Not sure if it's been released and certainly not yet in sync with the doc. – jfriend00 Jun 30 '18 at 06:35
  • 1
    @jfriend00 FYI, just stumbled upon [this comment](https://github.com/nodejs/node/issues/15413#issuecomment-398817793). Current Node `master` uses fs.promises. – Estus Flask Jul 15 '18 at 11:54
  • @estus - Yeah, it seems they haven't really made up their mind yet about how it should be done across all the modules. – jfriend00 Jul 15 '18 at 14:47
1

Use can use mz :

var fs = require('mz/fs');
hong4rc
  • 3,999
  • 4
  • 21
  • 40
  • 1
    This isn't the point of the question. There are lots of ways to "promisfy" the existing fs module or add-on modules to do that. The question is now to use the built-in promises. – jfriend00 Jun 30 '18 at 03:36
  • That is incredibly simple and easy to overlook - thank you – Michael Nelles Feb 14 '19 at 21:47