Node JS Promise.all 和 forEach [英] Node JS Promise.all and forEach
本文介绍了Node JS Promise.all 和 forEach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似数组的结构,它公开了异步方法.异步方法调用返回数组结构,这些结构反过来公开更多异步方法.我正在创建另一个 JSON 对象来存储从该结构获得的值,因此我需要小心跟踪回调中的引用.
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.
我编写了一个蛮力解决方案,但我想学习一个更惯用或更干净的解决方案.
I have coded a brute force solution, but I would like to learn a more idiomatic or clean solution.
- 对于 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();
推荐答案
很简单,有一些简单的规则:
It's pretty straightforward with some simple rules:
- 每当您在
then
中创建承诺时,将其返回 - 任何未返回的承诺都不会在外部等待. - 每当您创建多个 Promise 时,
.all
它们 - 这样它就会等待所有 Promise 并且不会隐藏任何来自其中的错误. - 每当你嵌套
then
时,你通常可以在中间返回 -then
链通常最多 1 级深.莉> - 无论何时执行 IO,都应该带有承诺 - 要么应该在承诺中,要么应该使用承诺来表示其完成.
- 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.
还有一些提示:
- 使用
.map
进行映射比使用for/push
更好 - 如果您使用函数映射值,map
让您可以简明扼要地表达一个一个地应用操作并聚合结果的概念. - 如果免费的话,并发比顺序执行要好 - 最好是并发执行并等待它们
Promise.all
比一个接一个地执行事情更好 - 每个在下一个之前等待.
- 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.
好的,让我们开始吧:
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]
});
这篇关于Node JS Promise.all 和 forEach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文