节点JS Promise.all和forEach

我有一个像暴露asynchronous方法结构的数组。 asynchronous方法调用返回数组结构,依次公开更多的asynchronous方法。 我正在创build另一个JSON对象来存储从这个结构获得的值,所以我需要注意跟踪callback中的引用。

我已经编写了一个powershell解决scheme,但我想学习一个更习惯或干净的解决scheme。

  1. 该模式应该是可重复的n层嵌套。
  2. 我需要使用promise.all或一些类似的技巧来确定何时解决封闭的例程。
  3. 并不是每个元素都必然会涉及到一个asynchronous调用。 所以在一个嵌套promise.all我不能简单地分配给我的基于索引的JSON数组元素。 不过,我确实需要在嵌套的forEach中使用promise.all之类的东西,以确保在parsing封装程序之前完成所有属性分配。
  4. 我正在使用蓝鸟许诺lib,但这不是一个要求

这是一些部分代码 –

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(); 

有一些简单的规则很简单:

  • 每当你在一个时候创造一个承诺,就把它退回来 – 任何你不回的承诺都不会在外面等待。
  • 无论何时你创造了多重承诺,都要等待所有的承诺,没有任何一个的错误被压制。
  • 每当你嵌套时,你通常可以返回中间then链通常是至多1层深。
  • 每当你执行IO时,它应该是一个承诺 – 要么应该是承诺 ,要么用一个承诺来表示完成。

还有一些提示:

  • 使用.map比使用for/push更好地完成映射 。如果使用函数映射值, map可以简洁地expression逐个应用操作并汇总结果的概念。
  • 如果它是免费的,并发性比顺序执行要好 – 最好是并发执行并等待它们Promise.all不是一个接一个地执行 – 每个都在下一个之前等待。

好的,让我们开始吧:

 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] }); 

下面是一个使用reduce的简单例子。 它连续运行,维护插入顺序,并不需要蓝鸟。

 /** * * @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()); } 

像这样使用它:

 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'); }); 

我们发现发送一个可选的上下文到循环是有用的。 上下文是可选的,并由所有迭代共享。

 function forEachPromise(items, fn, context) { return items.reduce(function (promise, item) { return promise.then(function () { return fn(item, context); }); }, Promise.resolve()); } 

你的承诺function如下所示:

 function logItem(item, context) { return new Promise((resolve, reject) => { process.nextTick(() => { console.log(item); context.itemCount++; resolve(); }) }); } 

我也遇到了同样的情况。 我解决了使用两个Promise.All()。

我认为是非常好的解决scheme,所以我把它发布在npm上: https ://www.npmjs.com/package/promise-foreach

我认为你的代码会是这样的

 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) })