javascript - What is the correct way to have a method return a dummy promise with Bluebird in node.js -


so, have function chain of promises this:

myclass.prototype.initasync = function() {   somefunctionasync(params).bind(this)     .then(function(data) {       processdata(data);       return someotherfunctionasync(params).bind(this);     })     .then(function(data) {       processdata(data);       return yetanotherfunctionasync(params).bind(this);     })     .finally(function(data) {       processdata(data);     });  } 

it works, want function able in chain of promises. normally, i'd rid of "finally" @ end, don't want caller responsible calling processdata. want able call function this:

setupfunctionasync(params).bind(this)   .then(function(data) {     processsetup();     return initasync();   })   .finally(function(data) {     runprogram();   }); 

in "finally" of initasync, proper thing do? want create new empty promise promise.new , return that? or want use promise.method empty method? or there better way?

right doing following wonder if best thing do:

return somefunctionasync(params).bind(this)    .then(function(data) { ...    .then(function(data) {      processdata(data);      return new promise(function(resolve, reject) { resolve(); });    }); 

newest update:

after reading of answers, work?

myclass.prototype.initasync = function() {   return somefunctionasync(params).bind(this)     .then(function(data) {       processdata(data);       return someotherfunctionasync(params).bind(this);     })     .then(function(data) {       processdata(data);       return yetanotherfunctionasync(params).bind(this);     })     .then(function(data) {       processdata(data);     });   } 

and called like:

this.initasync()   .then(function() {     runprogram();   }); 

notice don't have "return" in last of initasync. ok?

i think misunderstanding finally. function passed finally invoked in case, means both, when promise resolved , when rejected. finally returns promise (having value of previous promise), chaining no problem.

it looks lot should using then , using finally because seems fit last step in process. can use then here.

anyway, since finally returns promise can add return @ beginning of function. don't need "dummy promise".

not having return statement in function passed ot last then not problem. means no value passed next promise chained after then. however, called anyway. code added question looks fine.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -