q.allSettled 嵌套异步 forEach

q.allSettled with nested async forEach

我处于这样一种情况,在我继续之前,我必须等待 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[=18= 之后调用]已执行

我认为您需要将内部承诺与外部承诺联系起来,在这种情况下,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);
                });
            });
    });
}));