Node.js - 在 promise.all() 之后为每个结果继续承诺链
Node.js - Continue promise chain for each result after promise.all()
我在承诺链中使用 Promise.all()
。 Promise.all() returns 中的每个 promise 一个字符串。
我遇到的问题是 Promise.all() returns a Promise 反对下一个承诺,我想继续每个字符串的承诺链。
举个例子:
....
return Promise.all(plugins);
})
.then(function(response) {
console.log(response)
....
response
看起来像:
[ 'results from p1', 'results from p2' ]
有什么方法可以继续每个结果的承诺链,而不是继续使用包含所有结果的单个对象?
Promise.all 期望一系列承诺。所以插件是一系列承诺,更重要的是:插件是一个承诺。
所以你可以链接你的插件 Promise。
这将因此成为
Promise.all(plugins.map(function(plugin){
return plugin.then(function(yourPluginString){
return 'example '+ yourPluginString;
})
}))
Promise.all()
,根据其设计 returns,已解决值的单个承诺是您传递给它的所有承诺的已解决值数组。这就是它的作用。如果这不是您想要的,那么您可能使用了错误的工具。您可以通过多种方式处理单个结果:
首先,您可以循环遍历返回结果的数组,然后对它们做任何您想做的事以进行进一步处理。
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()
处理程序附加到每个单独的承诺,并在每个结果发生时处理它。
您可以使用 https://github.com/Raising/PromiseChain
这样的工具
并实现你所说的
//sc = internalScope
var sc = {};
new PromiseChain(sc)
.continueAll([plugin1,plugin2,plugin3],function(sc,plugin){
return plugin(); // I asume this return a promise
},"pluginsResults")
.continueAll(sc.pluginsResults,function(sc,pluginResult){
return handlePluginResults(pluginResult);
},"operationsResults")
.end();
我没有测试代码,有问题PM我
我在承诺链中使用 Promise.all()
。 Promise.all() returns 中的每个 promise 一个字符串。
我遇到的问题是 Promise.all() returns a Promise 反对下一个承诺,我想继续每个字符串的承诺链。
举个例子:
....
return Promise.all(plugins);
})
.then(function(response) {
console.log(response)
....
response
看起来像:
[ 'results from p1', 'results from p2' ]
有什么方法可以继续每个结果的承诺链,而不是继续使用包含所有结果的单个对象?
Promise.all 期望一系列承诺。所以插件是一系列承诺,更重要的是:插件是一个承诺。
所以你可以链接你的插件 Promise。
这将因此成为
Promise.all(plugins.map(function(plugin){
return plugin.then(function(yourPluginString){
return 'example '+ yourPluginString;
})
}))
Promise.all()
,根据其设计 returns,已解决值的单个承诺是您传递给它的所有承诺的已解决值数组。这就是它的作用。如果这不是您想要的,那么您可能使用了错误的工具。您可以通过多种方式处理单个结果:
首先,您可以循环遍历返回结果的数组,然后对它们做任何您想做的事以进行进一步处理。
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()
处理程序附加到每个单独的承诺,并在每个结果发生时处理它。
您可以使用 https://github.com/Raising/PromiseChain
这样的工具并实现你所说的
//sc = internalScope
var sc = {};
new PromiseChain(sc)
.continueAll([plugin1,plugin2,plugin3],function(sc,plugin){
return plugin(); // I asume this return a promise
},"pluginsResults")
.continueAll(sc.pluginsResults,function(sc,pluginResult){
return handlePluginResults(pluginResult);
},"operationsResults")
.end();
我没有测试代码,有问题PM我