试图了解如何promisification与BlueBird工作

我正在尝试使用Node.js的Bluebird库包裹我的头。

下面是一个简单的例子,不能像我所期望的那样工作。

var Promise = require("bluebird"); var myObj = { add: function(op1, op2) { return op1 + op2; } }; // Sync call to add method -> 7 console.log(myObj.add(3,4)); var myObjAsync = Promise.promisifyAll(myObj); // Async call to promisified add method -> nothing written to console myObjAsync.addAsync(2,3).then(function(data) { console.log(data); return data; }) 

我要么承诺或与蓝鸟丢失一些(主要)的概念。

在此先感谢您的帮助。

编辑:修改(现在的工作版本)基于jfriend00的反馈。

 var Promise = require("bluebird"); var myObj = { add: function(op1, op2) { return op1 + op2; } , add2: function(op1, op2, callback) { callback(null, op1 + op2); } }; // Sync call to add method -> 7 console.log(myObj.add(3,4)); var myObjAsync = Promise.promisifyAll(myObj); // Async call to promisified add method -> nothing written to console myObjAsync.addAsync(2,3).then(function(data) { console.log("%j", data); return data; }) // Async call to promisified add2 method -> 5 myObjAsync.add2Async(2,3).then(function(data) { console.log("%j", data); return data; }) 

为了使promisifyAll()工作,函数必须是asynchronous的,传递给函数的最后一个参数必须是完成callback,并且完成callback必须有第一个参数,错误参数在没有错误时是falsey返回值作为第二个参数(如果有值)。

你的function不符合任何这些标准。


下面是一个摘自蓝鸟文档的.promisifyAll()

假定目标方法符合node.js作为最后一个参数接受callback的callback约定,并将错误作为第一个参数和第二个参数的成功值调用该callback。 如果节点方法使用多个成功值调用其callback,则履行值将是它们的一个数组。

请记住, .promisifyAll()不能进行asynchronous操作的同步操作。 什么是做一个符合特定的调用约定的asynchronous操作,并通过挂钩callback和testingcallback的参数来检测成功或失败并传播返回值,将其包含在promise中。


如果你对蓝鸟是如何做这件事感到好奇的,你可以在Github上查看他们的实际代码,但是如果没有一些重要的研究,就不太容易遵循它的function。

这里有一个promisify函数的一个简单版本,看看它是什么(我build议使用蓝鸟的所有其他function,而不是这个)。

 // -------------------------------------------------------------- // promisify(fn, obj) // // Pass an async function that takes as its last argument a callback // that conforms to the node.js callback calling convention function(err, result) // passing obj is optional. If present the function passed in will be called // as obj.method() // // Returns: New function that when called will return a promise. // -------------------------------------------------------------- function promisify(fn, obj) { if (typeof fn !== "function") { throw new Error("fn argument to promisify() must be function"); } // obj is optional and may be undefined // if present, it will be used as context to call fn as in obj.fn() return function(/* args */) { // make copy of arguments object into a real array in a way that // does not prevent interpreter optimizations var args = new Array(arguments.length); for (var i = 0; i < args.length; i++) { args[i] = arguments[i]; } return new Promise(function(resolve, reject) { // add our callback function at the end of the args list var resultMany; args.push(function(err, result) { if (err) { reject(err); } else { // if 0 or 1 result, then just return it as a simple value if (arguments.length <= 2) { resolve(result); } else { // if more than one result came with the callback function, // then put it into an array so we can resolve with a single value (the array of results) // skip the first argument which is the err value resultMany = new Array(arguments.length - 1); for (var i = 0; i < arguments.length - 1; i++) { resultMany[i] = arguments[i + 1]; } resolve(resultMany); } } }); // call original function with our callback as last argument fn.apply(obj, args); }); } } 

这是promisify()函数的一个工作演示: https : promisify()