javascript - Run 2 promises and wait for both for "done()" -


i'm trying run 2 different functions using promises (with q.js) @ same time, , wait response of both run third action.

i this:

run( promise1, promise2).done( callbackforboth ); 

how can do?

you can use q.all function, this

q.all([promise1, promise2]).then(callbackforboth); 

normally, q.all followed .spread convenience method, spreads result q.all call, function parameters, this

q.all([promise1, promise2]).spread(function(pro1result, pro2result) {     return callbackforboth(); }); 

but problem method is, if of promises rejected, rest of promises not invoked.

so, if want make sure promises either fulfilled/rejected, can use q.allsettled in case

q.allsettled([promise1, promise2]).then(callbackforboth); 

here can use spread convenience method, little more granularity. each promise, object, has state attribute let subsequent consumers know if promise fulfilled or rejected. so, might want use spread this

q.allsettled([promise1, promise2]).spread(function(pro1result, pro2result) {     if (pro1result.state === "fulfilled" && pro2result.state === "fulfilled") {         return callbackforboth();     } else {         throw new error("not of them successful");     } }); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -