【问题标题】:q.allSettled with nested async forEachq.allSettled 与嵌套异步 forEach
【发布时间】:2017-04-01 04:00:42
【问题描述】:

在我继续之前,我必须等待 forEach 循环中的所有承诺得到解决。如果嵌套层只有一层,那将是在公园里散步。就我而言,我什至必须等待承诺中的承诺,然后才能转移到 q.allSettled。粗略的代码如下:

        return q.Promise(function (resolve, reject) {
        mydata.forEach(function (item) {
            products.forEach(function (product) {
                    var attributeSetNode = item.Products.Product.AttributeSets;
                        var promise = somePromise(), rankNode;
                        matchingPromises.push(promise);
                        debug("matchingpromise length before grab category: "+ matchingPromises.length);
                        //async function inside loop needs to be passed references
                        (function (product, rankNode, attributeSetNode) {
                            promise.then(function (grabbed) {
                                debug('Grabbed Category', grabbed);
-------------------- Problem Line --------------------
                                (function (product) {
                                    var dimsAndFeePromise = somePromise();
                                    matchingPromises.push(dimsAndFeePromise);
                                    debug("matchingpromise length after grab category: "+ matchingPromises.length);
                                    dimsAndFeePromise.then(function () {
                                        //Some future logic here. Once streamlined, this is actually supposed to return the calculations
                                        //here and not play with the reference itself inside the function call:(
                                        debug('Done with ASIN: ' + product.ASIN);
                                    });
                                })(product);
                            }).catch(function (err) {
                                debug(err);
                            })
                        })(product, rankNode, attributeSetNode);
            });
        });
        debug("Going to resolve allSettled matchingPromises with length: "+ matchingPromises.length);

------------------ Problem Line 2 -----------------------
        q.allSettled(matchingPromises).then(function (result) {
            resolve();
        });
    });

我只是不确定如何等待上述 for 循环,以便 问题行 2 仅在 问题行 1 执行后调用

【问题讨论】:

    标签: javascript promise q


    【解决方案1】:

    我认为您需要将内部承诺与外部承诺挂钩,在这种情况下,dimsAndFeePromise 需要与matchingPromises 挂钩。

    下面的代码应该让你朝着正确的方向前进:

    return q.Promise(function (resolve, reject) {
        mydata.forEach(function (item) {
            products.forEach(function (product) {
                var attributeSetNode = item.Products.Product.AttributeSets;
                var promise = somePromise(), 
                    rankNode,
                    enchainedPromise;
    
                debug("matchingpromise length before grab category: "+ matchingPromises.length);
                //async function inside loop needs to be passed references
                (function (product, rankNode, attributeSetNode) {
                    enchainedPromise = promise.then(function (grabbed) {
                        debug('Grabbed Category', grabbed);
    -------------------- Problem Line --------------------
                        return (function (product) {
                            var dimsAndFeePromise = somePromise();
                            // matchingPromises.push(dimsAndFeePromise);
                            debug("matchingpromise length after grab category: "+ matchingPromises.length);
                            return dimsAndFeePromise.then(function () {
                                //Some future logic here. Once streamlined, this is actually supposed to return the calculations
                                //here and not play with the reference itself inside the function call:(
                                debug('Done with ASIN: ' + product.ASIN);
                            });
                        })(product);
                    }).catch(function (err) {
                        debug(err);
                    })
                })(product, rankNode, attributeSetNode);
                matchingPromises.push(enchainedPromise);
            });
        });
        debug("Going to resolve allSettled matchingPromises with length: "+ matchingPromises.length);
    
    ------------------ Problem Line 2 -----------------------
        q.allSettled(matchingPromises).then(function (result) {
            resolve();
        });
    });
    

    我认为代码也可以分解为以下内容:

    return q.allSettled(mydata.map(function (item) {
        return products.map(function (product) {
            var attributeSetNode = item.Products.Product.AttributeSets;
            var promise = somePromise(), 
                rankNode;
    
                return promise.then(function (grabbed) {
                    return somePromise().then(function () {
                        //Some future logic here. Once streamlined, this is actually supposed to return the calculations
                        //here and not play with the reference itself inside the function call:(
                        debug('Done with ASIN: ' + product.ASIN);
                    });
                });
        });
    }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2016-08-18
      • 2020-04-12
      • 2011-04-10
      相关资源
      最近更新 更多