问题描述:

I was just wondering if anyone has any good ideas about a "ensuring readiness" pattern in Node JS when using promises. I have something like this, but I think the main problem with it is the fact that I think the .then(cb) for a promise actually overwrites the previous one, rather than chaining another handler...

 function awaitQueueCreation() {

if (!q._queueURL) return whenQueueCreated;

else return p.resolve(q._queueURL);

}

q.someQueueMethod = function(param) {

awaitQueueCreation().then(function() {

// do what this method is supposed to do...

});

};

How would you handle this type of thing?

网友答案:

Adding an additional .then() handler onto the same promise just creates a second notification for the same promise. It does not chain onto the previous .then() handler. It does not overwrite any prior .then() handler.

So adding two .then() handlers just sequences two callbacks one after the other when the promise is fullfilled.

If the first .then() handler itself returns an unfullfilled promise, the second .then() handler is still called right away (it is not "chained" to the new unfullfilled promise).

To chain two promises, you must do something like this:

p.then(...).then(...)

Not:

p.then(...)
p.then(...)
相关阅读:
Top