Using async/await + Bluebird to promisifyAll

14,364

Solution 1

I'm constructing a library that uses async/await, and I would like to know if it is possible to use native modules like fs with the async/await.

Yes. You can do it even simpler with Bluebird than in your example:

let fs = Promise.promisifyAll(require('fs'));

// and in async function:
let contents = await fs.readFileAsync('file.txt', 'utf-8');

Note that you need to add Async at the end of the method names.

Or you can use the mz module, without needing to add Async to the methods. See:

There are many modules that you can require once you npm install mz - for example you can require('mz/fs') and it instantly lets you use the fs module version that returns promises instead of taking callbacks. Combined with async await it lets you do things like:

let fs = require('mz/fs');

// and in async function:
let contents = await fs.readFile('file.txt', 'utf-8');

The above code is still non-blocking.

See this answer where I show an example of mz version of the crypto module and explain it in more detail:

See example:

let crypto = require('mz/crypto');

async function x() {
  let bytes = await crypto.randomBytes(4);
  console.log(bytes);
}

You can do the same with many other modules, including:

  • child_process
  • crypto
  • dns
  • fs
  • readline
  • zlib

I know that async/await it's just Promises in the background, so... is there a native way to promisify a method or function?

Soon Node.js will support that natively - see PR #5020 Adding Core support for Promises:

but in the meantime you can use mz.

For more context see also the Issue #7549 v1: executing async functions without callbacks should return promises:

See also the Node.js's Promises Working Group Repository:

Update: It seems that the PR 5020 mentioned above is not going to land in Node.js any time soon - thanks to Benjamin Gruenbaum for pointing it out in the comments. So it seems that using Bluebird's promisify and promisifyAll and the helpful mz module will be the only easy way to use modern features of the language with the core modules of Node.js. Fortunately they work very well, so it's not a big problem.

Solution 2

By all means, Bluebird is designed to work with native promises. The use case you describe is not only supported - but is a design goal of Bluebird.

Bluebird's promises implement then according to the Promises/A+ specification, which is guaranteed to work with await. Moreover, you can pass native promises to Bluebird and it'll work just fine.

Solution 3

Using Bluebird as well as Promises is only increasing your overhead. It means Bluebird is sufficient enough to handle other promises.

Share:
14,364

Related videos on Youtube

Max
Author by

Max

Updated on September 15, 2022

Comments

  • Max
    Max over 1 year

    I'm constructing a library that uses async/await, and I would like to know if it is possible to use native modules like fs with the async/await. I know that async/await it's just Promises in the background, so is there a native way to promisify a method or function? Currently I'm using the Bluebird, but I don't know if it's a bad pattern.

    Example:

        const Bluebird = require("bluebird");
        const { access } = require("fs");
    
        const accessAsync = Bluebird.promisify(access);
    
        async function checkInput(options) {
          await accessAsync(options.file);
          /// etc
          return options;
        }
    
        module.exports = (options) => {
          Promise.resolve(options)
            .then(checkInput)
        };
    

    I'm combining both native Promises and Bluebird. Should I only use Bluebird?

  • Admin
    Admin almost 7 years
    brilliant ideia!
  • Benjamin Gruenbaum
    Benjamin Gruenbaum almost 7 years
    I hate to be a party pooper - #5020 isn't happening anytime soon. I founded the promises working group - sadly - it's not going anywhere at the moment due to the sheer amount of work involved. There is a chance util.awaitable will land though in the coming months.
  • rsp
    rsp almost 7 years
    @BenjaminGruenbaum Thanks for the info. I updated the answer.
  • rsp
    rsp almost 7 years
    @celoxxx See my updated answer. I adder more info on Bluebird and the status of the pull request adding promise support to Node.