使用 async/await + Bluebird 来 promisifyAll [英] Using async/await + Bluebird to promisifyAll

查看:36
本文介绍了使用 async/await + Bluebird 来 promisifyAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用 async/await 的库,我想知道是否可以将 fs 等本机模块与 一起使用异步/等待.我知道 async/await 它只是后台的 Promises,那么是否有一种本地方式来保证方法或函数?目前我正在使用 Bluebird,但我不知道这是否是一个糟糕的模式.

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.

示例:

    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)
    };

我结合了原生 Promise 和 Bluebird.我应该只使用 Bluebird 吗?

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

推荐答案

我正在构建一个使用 async/await 的库,我想知道是否可以将 fs 等本机模块与 async/await 一起使用.

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.

是的.你可以用 Bluebird 比你的例子更简单:

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');

注意你需要在方法名称的末尾添加Async.

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

或者您可以使用 mz 模块,而无需将 Async 添加到方法中.见:

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

一旦你npm install mz,你就可以需要很多模块——例如你可以require('mz/fs'),它可以立即让你使用fs 返回承诺而不是回调的模块版本.结合 async await 它可以让您执行以下操作:

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.

请参阅此答案,其中我展示了 crypto 模块的 mz 版本示例,并对其进行了更详细的解释:

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

参见示例:

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
  • 加密
  • dns
  • fs
  • readline
  • zlib

我知道 async/await 只是后台的 Promises,所以......是否有一种本地方式来承诺方法或函数?

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

很快,Node.js 将在本机支持这一点 - 请参阅 PR #5020 添加对 Promises 的核心支持:

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

但同时你可以使用mz.

有关更多上下文,另请参阅问题 #7549 v1:在没有回调的情况下执行异步函数应返回承诺:

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

另请参阅 Node.js 的Promises 工作组存储库:

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

更新: 上面提到的 PR 5020 似乎不会很快登陆 Node.js - 感谢 Benjamin Gruenbaum 在评论中指出这一点.因此,似乎使用 Bluebird 的 promisifypromisifyAll 以及有用的 mz 模块将是使用该语言的现代特性的唯一简单方法Node.js 的核心模块.幸运的是,它们运行良好,所以问题不大.

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.

这篇关于使用 async/await + Bluebird 来 promisifyAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆