使用异步/等待+蓝鸟来PromisifyAll [英] Using async/await + Bluebird to promisifyAll

查看:119
本文介绍了使用异步/等待+蓝鸟来PromisifyAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用async/await的库,我想知道是否可以将fs之类的本机模块与async/await一起使用.我知道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)
    };

我将原生Promises和Bluebird结合在一起.我应该只使用Bluebird吗?

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

推荐答案

我正在构建一个使用async/await的库,我想知道是否可以在async/await中使用诸如fs之类的本机模块.

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模块版本,该版本返回promise而不是进行回调.与异步等待结合使用,您可以执行以下操作:

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
  • crypto
  • dns
  • fs
  • readline
  • zlib
  • child_process
  • crypto
  • 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?

Soon Node将本身提供支持-请参阅 PR#5020为承诺添加核心支持:

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

,但与此同时,您可以使用mz.

but in the meantime you can use mz.

有关更多上下文,请参见问题#7549 v1:执行不带回调的异步函数应返回promise :

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

另请参阅节点的承诺工作组存储库:

更新:似乎上述PR 5020不会很快降落在Node上,这要感谢Benjamin Gruenbaum在评论中指出.因此,似乎使用Bluebird的promisifypromisifyAll以及有用的mz模块将是将语言的现代功能与Node的核心模块一起使用的唯一简便方法.幸运的是,它们工作得很好,所以这不是什么大问题.

Update: It seems that the PR 5020 mentioned above is not going to land in Node 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. Fortunately they work very well so it's not a big problem.

这篇关于使用异步/等待+蓝鸟来PromisifyAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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