【问题标题】:Node JS Promise.all and forEach节点 JS Promise.all 和 forEach
【发布时间】:2015-10-03 12:15:28
【问题描述】:

我有一个类似数组的结构,它公开了异步方法。异步方法调用返回数组结构,进而公开更多异步方法。我正在创建另一个 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();

【问题讨论】:

  • 这是我想要改进的工作源的链接。 github.com/pebanfield/change-view-service/blob/master/src/…
  • 我在示例中看到您正在使用 bluebird,在这种情况下,bluebird 实际上使用 Promise.map(并发)和 Promise.each(顺序)让您的生活更加轻松 ,另请注意 Promise.defer 已弃用 - 我的答案中的代码显示了如何通过 returning 承诺来避免它。 Promise 都是关于返回值的。

标签: javascript node.js asynchronous promise


【解决方案1】:

通过一些简单的规则非常简单:

  • 每当您在 then 中创建承诺时,将其返回 - 您未返回的任何承诺都不会在外面等待。
  • 每当您创建多个 Promise 时,.all它们 - 这样它会等待所有 Promise,并且不会消除任何错误。
  • 当你嵌套thens 时,通常可以在中间返回 - then 链通常最多 1 层深。
  • 无论何时执行 IO,都应该带有一个 Promise - 它应该在一个 Promise 中,或者它应该使用一个 Promise 来表示它的完成。

还有一些提示:

  • 使用.map 进行映射比使用for/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]
});

【讨论】:

  • 啊,从你的角度来看一些rules :-)
  • @Bergi 确实应该有人列出这些规则并简要介绍 Promise 的背景。我们大概可以在 bluebirdjs.com 上托管它。
  • 因为我不应该只说谢谢 - 这个例子看起来不错,我确实喜欢地图建议,但是,对于只有一些具有异步方法的对象集合该怎么办? (我上面的第 3 点)我有一个想法,我会将每个元素的解析逻辑抽象为一个函数,然后让它在异步调用响应中解析,或者在没有异步调用的地方简单解析。这有意义吗?
  • 我还需要让 map 函数返回我正在构建的 json 对象和我需要进行的异步调用的结果,所以也不知道该怎么做 - 最后整个事情都需要递归,因为我正在遍历目录结构 - 我仍在研究这个,但有偿工作正在阻碍:(
  • @user3205931 promises 是simple, rather than easy,也就是说 - 它们不像其他东西那么熟悉,但是一旦你了解它们,它们就会更好地使用。坚持住你会明白的:)
【解决方案2】:

这是一个使用 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());
}

并像这样使用它:

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

你的 promise 函数应该是这样的:

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

【讨论】:

    【解决方案3】:

    我也经历过同样的情况。我用两个 Promise.All() 解决了。

    我认为是非常好的解决方案,所以我在 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)
        })
    

    【讨论】:

      【解决方案4】:

      只是为了添加到所提供的解决方案中,就我而言,我想从 Firebase 获取多个数据以获取产品列表。这是我的做法:

      useEffect(() => {
        const fn = p => firebase.firestore().doc(`products/${p.id}`).get();
        const actions = data.occasion.products.map(fn);
        const results = Promise.all(actions);
        results.then(data => {
          const newProducts = [];
          data.forEach(p => {
            newProducts.push({ id: p.id, ...p.data() });
          });
          setProducts(newProducts);
        });
      }, [data]);
      

      【讨论】:

        猜你喜欢
        • 2018-05-11
        • 2018-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-29
        相关资源
        最近更新 更多