q.all与嵌套的async forEach一起建立

时间:2017-03-31 19:26:41

标签: javascript promise q

我处于这种情况,我必须等待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行执行后调用

1 个答案:

答案 0 :(得分:3)

我相信你需要用内在的承诺来包含内在的承诺,在这种情况下,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);
                });
            });
    });
}));