javascript - I'm trying to use the Bluebird's method Promisify and it's not working -


i'm not able make simple example works on bluebird. i've used new promise method , works, when try use promisify method may doing wrong.

exports.makerequest = function(sometext){     return promise.resolve(sometext); }  var makerequestasync = promise.promisify(exports.makerequest);  makerequestasync('testing').then(function(data){     console.log(data); // --> log never appears }); 

i want understand how promisify works.

bluebird's promisify() works node-style functions take callback last argument callback takes 2 arguments, error , data. function not meet criteria.

you can read how works here.

in addition, there's no need promisify function returns promise. can call function directly , use returned promise since it's promise returning function.

exports.makerequest = function(sometext){     return promise.resolve(sometext); }  exports.makerequest('testing').then(function(data){     console.log(data); }); 

working demo: http://jsfiddle.net/jfriend00/n01zyqc6/


of course, since function isn't async, there doesn't appear reason use promises @ all.


here's example of promisifying async , uses right calling convention:

exports.myfunc = function(data, callback) {     // make result async     settimeout(function() {         // call callback node style calling convention         callback(0, data);     }, 500);  };  var myfuncasync = promise.promisify(exports.myfunc);  myfuncasync("hello").then(function(result) {     console.log(result); }); 

Comments