node.js - bluebird promisifyAll() and then() not working together with node require -


i'm using promises first time, please bear me.

basically, i'm not seeing function in .then() statement being called.

when call t.t(), working correctly.

when call t.tasync(), t() again called.

however result isn't being passed when call t.taync().then(console.log);

here node module:

'use strict'; var t = function(){     console.log('inside t()');     return 'j'; };  module.exports = {     t: t }; 

and here demo script:

'use strict';  require('should'); var promise = require('bluebird'); var t = require('../src/t');  promise.promisifyall(t);  /*     call t() once demonstrate.     call tasync() , see t() called     call tasync.then(fn), , isn't called    */   // works expected, calling t() console.log('calling t()...' + t.t());  // works, calling t() t.tasync();  // then() statement isn't called t.tasync().then(function(res){     // expect called     console.log('hhhuuuzzzaaahhh' + res); });   /*     keep script running 5 seconds  */  (function (i) {     settimeout(function () {         console.log('finished program');     }, * 1000) })(5); 

and here output test:

inside t() calling t()...j inside t() inside t() finished program 

your then clause never called because tasync expecting t call callback not return value.

promisifyall wraps node.js aysynchronous api's function wrapping needs conform node.js callback signature:

function t(callback) {   callback(null, 'j'); } 

however, suspect based on code not want promiseifyall instead try() or method():

function t() {   return 'j'; }  promise.try(t).then(function(result) {   console.log(result); }); 

or

var t = promise.method(function() {   return 'j'; });  t().then(function(result) {   console.log(result); }); 

for contrast above bluebird.js specific. perform generic promises approach 1 of 2 ways:

using promise constructor:

function t() {   return new promise(function(resolve, reject) {     resolve('j');   }); }  t().then(function(result) {   console.log(result); }); 

or, use then chaining feature:

function t() {   return 'j'; }  promise.resolve()   .then(t)   .then(function(result) {     console.log(result);   }); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -