如何asynchronous方式调用10个函数并收集所有结果并知道何时完成?

我使用Node.js的延期模块,并创build了从远端服务器获取数据的延迟函数。 我需要从不同的遥远的服务器获取10个文件,如何做到这一点,承诺知道什么时候完成,然后获取所有结果在数组? 目前我已经closures了,而且只有在完成之前,我才能获取下一个文件,但是它是同步和慢速的。

根据我所假设的你正在使用的模块的文档 ,你可以这样做:

deferred(delayedAdd(2, 3), delayedAdd(3, 5), delayedAdd(1, 7))(function (result) {` console.log(result); // [5, 8, 8]` }); 

例如:

 deferred(promise1, promise2, promise3)(function (result) { // `result` is an array of the results }); 

在上面的链接,search“分组承诺”(虽然它没有比以上更多)。

如果你有一个promise的数组,你可以使用Function#apply来做以上的事情:

 deferred.apply(undefined, theArray)(function (result) { // `result` is an array of the results });