Promise.all()
,其設計將返回一個承諾誰的解析值是解決值的你通過它的所有承諾的數組。這就是它的作用。如果這不是你想要的,那麼也許你正在使用錯誤的工具。您可以用多種方式處理各個結果:
首先,您可以遍歷返回結果的數組,並執行任何您想要的操作以進行進一步處理。
Promise.all(plugins).then(function(results) {
return results.map(function(item) {
// can return either a value or another promise here
return ....
});
}).then(function(processedResults) {
// process final results array here
})
其次,你可以在你把它傳遞給Promise.all()
附加.then()
處理程序到每一個人的承諾。
// return new array of promises that has done further processing
// before passing to Promise.all()
var array = plugins.map(function(p) {
return p.then(function(result) {
// do further processing on the individual result here
// return something (could even be another promise)
return xxx;
});
})
Promise.all(array).then(function(results) {
// process final results array here
});
或者,如果你第三次並不關心,當所有的結果都做,你只是想單獨處理每一個,那麼就不要使用Promise.all()
可言。只需附加一個.then()
處理程序到每個單獨的承諾,並處理每個結果。
看一看[調用然後之前或之後Promise.all](http://stackoverflow.com/q/36594198/1048572) – Bergi