【发布时间】: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