节点JS Promise.all和forEach

use*_*931 105 javascript asynchronous node.js promise

我有一个类似于数组的数组,它暴露了异步方法.async方法调用返回数组结构,进而显示更多异步方法.我正在创建另一个JSON对象来存储从此结构获取的值,因此我需要小心跟踪回调中的引用.

我编写了一个强力解决方案,但我想学习一个更惯用或更干净的解决方案.

  1. 对于n级嵌套,该模式应该是可重复的.
  2. 我需要使用promise.all或类似的技术来确定何时解决封闭的例程.
  3. 并非每个元素都必然涉及进行异步调用.所以在嵌套的promise.all中我不能简单地根据索引对我的JSON数组元素进行赋值.尽管如此,我确实需要在嵌套的forEach中使用promise.all之类的东西,以确保在解析封闭例程之前已经完成了所有属性赋值.
  4. 我正在使用bluebird promise 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();
Run Code Online (Sandbox Code Playgroud)

Ben*_*aum 333

使用一些简单的规则非常简单:

  • 无论何时在a中创建一个promise,then都要返回 - 任何你不返回的promise都不会在外面等待.
  • 每当你创建多​​个promises时,.all它们就会等待所有的promise,并且没有任何错误会被沉默.
  • 每当你筑巢时then,你通常可以在中间返回 - then链通常最多1级深.
  • 无论何时执行IO,它都应该有一个承诺 - 它应该在一个承诺中,或者它应该使用一个承诺来表示它的完成.

还有一些提示:

  • 映射是更好地做.mapfor/push -如果你有一个函数映射值,map让你简洁地表达应用动作一招一式综合这些结果的概念.
  • 并发性优于顺序执行,如果它是免费的 - 最好同时执行并等待它们,而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]
});
Run Code Online (Sandbox Code Playgroud)

  • 啊,从你的角度来看一些[规则](http://stackoverflow.com/a/25756564/1048572):-) (5认同)
  • @ user3205931承诺是[简单,而不是简单](http://www.infoq.com/presentations/Simple-Made-Easy),也就是说 - 它们不像其他东西那么熟悉,但是一旦你弄清楚它们使用起来好多了.坚持你会得到它:) (2认同)

Ste*_*gin 38

这是一个使用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());
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

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');
});
Run Code Online (Sandbox Code Playgroud)

我们发现将可选上下文发送到循环中很有用.上下文是可选的,并由所有迭代共享.

function forEachPromise(items, fn, context) {
    return items.reduce(function (promise, item) {
        return promise.then(function () {
            return fn(item, context);
        });
    }, Promise.resolve());
}
Run Code Online (Sandbox Code Playgroud)

你的承诺函数看起来像这样:

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