如何等待两个承诺的结果,然后做某事?

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

嗯,主题。想不通...

条件:两个 promise 都是异步启动的,并且不是一个接一个。像这样:

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

DoSomethingAfterBothPromises()

P. S. 我使用 WinJS 的 promises,而不是 ES2015/ES2016,但它们没有太大差异。

如果 WinJS 承诺是 Promise/A+ 承诺

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

好吧,回答得太早了 - WinJS 承诺是微软 "let's do things just a little different" 对网络

态度的典型例子

感谢@Evan Trimboli .join!请参阅主题下的评论。我去了 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();