Javascript Node JS Promise.all 和 forEach
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31413749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node JS Promise.all and forEach
提问by user3205931
I have an array like structure that exposes async methods. The async method calls return array structures that in turn expose more async methods. I am creating another JSON object to store values obtained from this structure and so I need to be careful about keeping track of references in callbacks.
我有一个类似数组的结构,它公开了异步方法。异步方法调用返回数组结构,这些结构又公开更多异步方法。我正在创建另一个 JSON 对象来存储从这个结构获得的值,所以我需要小心跟踪回调中的引用。
I have coded a brute force solution, but I would like to learn a more idiomatic or clean solution.
我已经编写了一个蛮力解决方案,但我想学习一个更惯用或更干净的解决方案。
- The pattern should be repeatable for n levels of nesting.
- I need to use promise.all or some similar technique to determine when to resolve the enclosing routine.
- Not every element will necessarily involve making an async call. So in a nested promise.all I can't simply make assignments to my JSON array elements based on index. Nevertheless, I do need to use something like promise.all in the nested forEach to ensure that all property assignments have been made prior to resolving the enclosing routine.
- I am using the bluebird promise lib but this is not a requirement
- 对于 n 级嵌套,该模式应该是可重复的。
- 我需要使用 promise.all 或一些类似的技术来确定何时解决封闭例程。
- 并非每个元素都必然涉及进行异步调用。因此,在嵌套的 promise.all 中,我不能简单地根据索引对 JSON 数组元素进行分配。尽管如此,我确实需要在嵌套的 forEach 中使用 promise.all 之类的东西,以确保在解析封闭例程之前已经进行了所有属性分配。
- 我正在使用 bluebird promise lib 但这不是必需的
Here is some partial code -
这是一些部分代码 -
var jsonItems = [];
items.forEach(function(item){
var jsonItem = {};
jsonItem.name = item.name;
item.getThings().then(function(things){
// or Promise.all(allItemGetThingCalls, function(things){
things.forEach(function(thing, index){
jsonItems[index].thingName = thing.name;
if(thing.type === 'file'){
thing.getFile().then(function(file){ //or promise.all?
jsonItems[index].filesize = file.getSize();
回答by Benjamin Gruenbaum
It's pretty straightforward with some simple rules:
它非常简单,有一些简单的规则:
- Whenever you create a promise in a
then
, return it- any promise you don't return will not be waited for outside. - Whenever you create multiple promises,
.all
them- that way it waits for all the promises and no error from any of them are silenced. - Whenever you nest
then
s, you can typically return in the middle-then
chains are usually at most 1 level deep. - Whenever you perform IO, it should be with a promise- either it should be in a promise or it should use a promise to signal its completion.
- 每当您在 a 中创建承诺时
then
,将其返回- 您不返回的任何承诺都不会在外面等待。 - 每当您创建多个承诺时,
.all
它们- 这样它就会等待所有承诺并且没有任何错误被静音。 - 每当您嵌套
then
s 时,您通常可以在中间返回-then
链通常最多 1 级深。 - 每当您执行 IO 时,它都应该带有承诺- 要么应该在承诺中,要么应该使用承诺来表示其完成。
And some tips:
还有一些提示:
- Mapping is better done with
.map
than withfor/push
- if you're mapping values with a function,map
lets you concisely express the notion of applying actions one by one and aggregating the results. - Concurrency is better than sequential execution if it's free- it's better to execute things concurrently and wait for them
Promise.all
than to execute things one after the other - each waiting before the next.
- 映射
.map
比 with更好for/push
- 如果您使用函数映射值,map
可以让您简明地表达一个接一个应用操作并聚合结果的概念。 - 如果它是免费的,并发比顺序执行要好- 并发执行并等待它们
Promise.all
比一个接一个地执行更好- 每个都在下一个之前等待。
Ok, so let's get started:
好的,让我们开始吧:
var items = [1, 2, 3, 4, 5];
var fn = function asyncMultiplyBy2(v){ // sample async action
return new Promise(resolve => setTimeout(() => resolve(v * 2), 100));
};
// map over forEach since it returns
var actions = items.map(fn); // run the function over all items
// we now have a promises array and we want to wait for it
var results = Promise.all(actions); // pass array of promises
results.then(data => // or just .then(console.log)
console.log(data) // [2, 4, 6, 8, 10]
);
// we can nest this of course, as I said, `then` chains:
var res2 = Promise.all([1, 2, 3, 4, 5].map(fn)).then(
data => Promise.all(data.map(fn))
).then(function(data){
// the next `then` is executed after the promise has returned from the previous
// `then` fulfilled, in this case it's an aggregate promise because of
// the `.all`
return Promise.all(data.map(fn));
}).then(function(data){
// just for good measure
return Promise.all(data.map(fn));
});
// now to get the results:
res2.then(function(data){
console.log(data); // [16, 32, 48, 64, 80]
});
回答by Steven Spungin
Here's a simple example using reduce. It runs serially, maintains insertion order, and does not require Bluebird.
这是一个使用 reduce 的简单示例。它串行运行,维护插入顺序,并且不需要 Bluebird。
/**
*
* @param items An array of items.
* @param fn A function that accepts an item from the array and returns a promise.
* @returns {Promise}
*/
function forEachPromise(items, fn) {
return items.reduce(function (promise, item) {
return promise.then(function () {
return fn(item);
});
}, Promise.resolve());
}
And use it like this:
并像这样使用它:
var items = ['a', 'b', 'c'];
function logItem(item) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
console.log(item);
resolve();
})
});
}
forEachPromise(items, logItem).then(() => {
console.log('done');
});
We have found it useful to send an optional context into loop. The context is optional and shared by all iterations.
我们发现将可选上下文发送到循环中很有用。上下文是可选的并且由所有迭代共享。
function forEachPromise(items, fn, context) {
return items.reduce(function (promise, item) {
return promise.then(function () {
return fn(item, context);
});
}, Promise.resolve());
}
Your promise function would look like this:
您的承诺函数如下所示:
function logItem(item, context) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
console.log(item);
context.itemCount++;
resolve();
})
});
}
回答by saulsluz
I had through the same situation. I solved using two Promise.All().
我也经历过同样的情况。我用两个 Promise.All() 解决了。
I think was really good solution, so I published it on npm: https://www.npmjs.com/package/promise-foreach
我认为这是一个非常好的解决方案,所以我在 npm 上发布了它:https://www.npmjs.com/package/promise-foreach
I think your code will be something like this
我认为你的代码会是这样的
var promiseForeach = require('promise-foreach')
var jsonItems = [];
promiseForeach.each(jsonItems,
[function (jsonItems){
return new Promise(function(resolve, reject){
if(jsonItems.type === 'file'){
jsonItems.getFile().then(function(file){ //or promise.all?
resolve(file.getSize())
})
}
})
}],
function (result, current) {
return {
type: current.type,
size: jsonItems.result[0]
}
},
function (err, newList) {
if (err) {
console.error(err)
return;
}
console.log('new jsonItems : ', newList)
})