javascript - Given a set of Promises, how do I forcefully resolve with the response of the last Promise, while ensuring all Promises resolve successfully? -


promise 1 (resolves in 200ms) promise 2 (resolves in 400ms) promise 3 (resolves in 1000ms) promise 4 (resolves in 300ms) promise 5 (resolves in 500ms) 

these promises resolve in following order

0 - requests start 100 200 - promise 1 300 - promise 4 400 - promise 2 500 - promise 5 (most recent application state) 600 700 800 900 1000 - promise 3 (stagnant application state) 

given in app, response of promises dictates application state, when older promise resolves more newer one, application state stagnant.

a potential implementation not start next request until previous request has finished, hang user progress considerably.

edit:

i might have left out bit of necessary context. there's no way me tell when promise added. promise.all can't have items added after has started, promise.all might not work me.

for more normal use-case provided answers have worked nicely, unfortunately api bit chatty.

so want promises complete go forward newest promise's value?

promise.all([p1, p2, p3, p4, p5]) // resolves when promises have resolved .then((results) => {     // results array of resolved values }) 

would promise.all() work you? bear in mind rejects if of promises reject.

https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/promise/all


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -