dimanche 19 avril 2015

Bluebird: Waiting for one promise to settle

At the risk of sounding stupid: What's the most efficient way to wait for one promise to settle? Say I have promise A and I want to create promise B. The fulfillment of B does not depend on the final value of A. It should proceed even if A is rejected. The process shouldn't start, however, until A is settled one way or the other.


What I have currently looks like the following:



var prevResult;

function doStuff() {
if (prevResult) {
prevResult = promise.settle([ prevResult ]).then(function() {
return doStuff();
})
} else {
prevResult = updateDB().finally(function() {
prevResult = null;
});
}
return prevResult;
}


The code is a bit non-obvious. I'm also a bit worried about chaining a bunch of promises from settle() together, seeing how it's function for performing less trivial kind of coordination. Seems like there ought to be a simpler way of doing this.


Aucun commentaire:

Enregistrer un commentaire