How to wait for result of two promises, then do something?

佐手、 提交于 2019-12-31 03:46:47

问题


Well, subject. Can't figure out...

Condition: both promises start asynchronously and not one after another. Like this:

spashGoing.then(doSomethingForItself());
writingGoing.then(doSomethingForItself2());

DoSomethingAfterBothPromises()

P. S. I use promises from WinJS, not the ES2015/ES2016, but they don't have big defferences.


回答1:


If WinJS promises are Promise/A+ promises

Promise.all([
    spashGoing.then(doSomethingForItself), 
    writingGoing.then(doSomethingForItself2)
]).then(function(results) {
    // do things with results
});

Well, answered too soon - WinJS promises are a typical example of Microsoft's "let's do things just a little different" attitude towards the web




回答2:


Thanks @Evan Trimboli with .join! See in comment under topic. I went to this and found more beatiful solution here:

WinJS.Promise.join({ 
     p1: p1, 
     p2: p2, 
     m3: 3})
.then(function (args) {
    //args.p1 = null
    //args.p2 = promise with handle to file
    //args.m3 = 3
    console.log("Joined promise completed");
}, function (error) {
    console.log("Joined promise error '" + error + "' occured but was handled");
}).done();


来源:https://stackoverflow.com/questions/42467569/how-to-wait-for-result-of-two-promises-then-do-something

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!