node.js - Trying to understand how promisification works with BlueBird -
node.js - Trying to understand how promisification works with BlueBird -
i'm trying wrap head around promises using bluebird library node.js.
below simple illustration doesn't work expect.
var promise = require("bluebird"); var myobj = { add: function(op1, op2) { homecoming op1 + op2; } }; // sync phone call add together method -> 7 console.log(myobj.add(3,4)); var myobjasync = promise.promisifyall(myobj); // async phone call promisified add together method -> nil written console myobjasync.addasync(2,3).then(function(data) { console.log(data); homecoming data; })
i'm either missing (major) concept promises or bluebird.
thanks in advance help.
edit: revised (and working version) based on feedback jfriend00.
var promise = require("bluebird"); var myobj = { add: function(op1, op2) { homecoming op1 + op2; } , add2: function(op1, op2, callback) { callback(null, op1 + op2); } }; // sync phone call add together method -> 7 console.log(myobj.add(3,4)); var myobjasync = promise.promisifyall(myobj); // async phone call promisified add together method -> nil written console myobjasync.addasync(2,3).then(function(data) { console.log("%j", data); homecoming data; }) // async phone call promisified add2 method -> 5 myobjasync.add2async(2,3).then(function(data) { console.log("%j", data); homecoming data; })
for promisifyall()
work, function has asynchronous , lastly argument passed function has completion callback , completion callback has have first argument, error argument falsey when there's no error.
your function doesn't meet of criteria.
here's excerpt bluebird doc .promisifyall()
:
the target methods assumed conform node.js callback convention of accepting callback lastly argument , calling callback error first argument , success value on sec argument. if node method calls callback multiple success values, fulfillment value array of them.
please remember .promisifyall()
can't create synchronous operation async operation. take asynchronous operation conforms specific calling convention , wraps in promise hooking callback , testing callback's arguments observe success or failure , propagate homecoming value.
if you're curious how bluebird this, can inspect actual code here on github, though not super easy follow without important study.
here's bit simpler version of promisify function see (i'd recommend using bluebird of it's other features rather this).
// -------------------------------------------------------------- // promisify(fn, obj) // // pass async function takes lastly argument callback // conforms node.js callback calling convention function(err, result) // passing obj optional. if nowadays function passed in called // obj.method() // // returns: new function when called homecoming promise. // -------------------------------------------------------------- function promisify(fn, obj) { if (typeof fn !== "function") { throw new error("fn argument promisify() must function"); } // obj optional , may undefined // if present, used context phone call fn in obj.fn() homecoming function(/* args */) { // create re-create of arguments object real array in way // not prevent interpreter optimizations var args = new array(arguments.length); (var = 0; < args.length; i++) { args[i] = arguments[i]; } homecoming new promise(function(resolve, reject) { // add together our callback function @ end of args list var resultmany; args.push(function(err, result) { if (err) { reject(err); } else { // if 0 or 1 result, homecoming simple value if (arguments.length <= 2) { resolve(result); } else { // if more 1 result came callback function, // set array can resolve single value (the array of results) // skip first argument err value resultmany = new array(arguments.length - 1); (var = 0; < arguments.length - 1; i++) { resultmany[i] = arguments[i + 1]; } resolve(resultmany); } } }); // phone call original function our callback lastly argument fn.apply(obj, args); }); } }
here's working demo of promisify()
function: https://jsfiddle.net/jfriend00/m1265vos/
node.js bluebird
Comments
Post a Comment