javascript promisifyAll 如何工作,或者它的工作要求是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29595449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:46:06  来源:igfitidea点击:

How promisifyAll works, or what are the requirements for it work?

javascriptnode.jsnpmbluebird

提问by Muhammad Umer

In a promise library bluebird have function promisifyAll or other similar libraries that claim to convert async functions with callback patterns into promise based ie. resolve(), reject(), or done()..So how does it work?

在承诺库中,bluebird 有函数 promisifyAll 或其他类似的库,它们声称将具有回调模式的异步函数转换为基于承诺的 ie。resolve(), reject(), 或done()..那么它是如何工作的呢?

For example:

例如:

function myAsync1 (data, url, callBack) {...}

and if i put it in

如果我把它放进去

Promise.promisify(myAsycn1);

then will my function work like this..

那么我的函数会像这样工作吗..

myAsync1('{..}', 'http://..').then(function(){...});

This is have been bothering me. Is there a pattern that async non promise libs or function need to follow for Bluebird promisifyAll to convert them to promises based methods or there is some magic that converts them.

这一直困扰着我。Bluebird promisifyAll 是否需要遵循异步非承诺库或函数将它们转换为基于承诺的方法的模式,或者有一些魔法可以转换它们。

If not then what are the requirements and how does it work with existing libraries like mongodb etc.

如果不是,那么要求是什么以及它如何与 mongodb 等现有库一起使用。

回答by Aaron Dufour

Is there a pattern that async non promise libs or function need to follow for Bluebird promisifyAll to convert them to promises based methods

Bluebird promisifyAll 是否需要遵循异步非承诺库或函数的模式将它们转换为基于承诺的方法

Yes, there is a pattern. The functions it converts must expect a callback as their last argument. Additionally, it must pass an error as the first argument to the callback (nullif no error) and the return value as the second argument.

是的,有一种模式。它转换的函数必须期望回调作为它们的最后一个参数。此外,它必须将错误作为第一个参数传递给回调(null如果没有错误)和返回值作为第二个参数。

The BlueBird promisifyfunction is very difficult to follow because of optimizations, so I'll show a simple way it could be written:

promisify由于优化,BlueBird函数很难遵循,因此我将展示一种简单的编写方法:

function promisify(fn) {
  return function() {
    var that = this; // save context
    var args = slice.call(arguments); // turn into real array
    return new Promise(function(resolve, reject) {
      var callback = function(err, ret) { // here we assume the arguments to
                                          // the callback follow node.js
                                          // conventions
        if(err != undefined) {
          reject(err);
        } else {
          resolve(ret);
        }
      };
      fn.apply(that, args.concat([callback])); // Now assume that the last argument will
                                              // be used as a callback
    });
  };
}

Now we could implement promisifyAllby looping over the functions in the target object and using promisifyon each one.

现在我们可以promisifyAll通过循环目标对象中的函数并promisify在每个函数上使用来实现。

回答by Lior Erez

The promisifyAll()method promisifies the entire module or object which is being called as a parameter. What it means is that a copy of each property of the object is created with Asyncsuffix, which is actually a promisified version of the same method, i.e you can use the .then()or .done()methods on it .

promisifyAll()方法承诺作为参数调用的整个模块或对象。意思是对象的每个属性都创建了一个Async后缀的副本,这实际上是相同方法的promisified版本,即你可以在其上使用.then()or.done()方法。

For example, if you have a doSomething()method in someModulemodule, after calling Promise.promisifyAll(someModule)a new method will be created in the module called doSomethingAsync(). You can use it this way:

例如,如果您doSomething()someModule模块中有一个方法,则在调用后Promise.promisifyAll(someModule)会在被调用的模块中创建一个新方法doSomethingAsync()。你可以这样使用它:

var someModule = require('some-module');
Promise.promisifyAll(someModule);
someModule.doSomethingAsync().then(function(result) {
    // do whatever you want with result, this function runs after doSomthingAsync() 
    // is finished and the returned value is stored in 'result' variable.
});

Check out the bluebird API documentationfor more information.

查看bluebird API 文档了解更多信息。