0
我處於一種情況,我必須等待forEach循環中的所有承諾才能繼續執行。如果嵌套層次只有一層,那該是在公園散步了。就我而言,我必須等待承諾中的承諾,然後才轉到q.allSettled。下面粗略碼給出:q.all安裝嵌套異步forEach
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循環後問題行1已執行
哇,非常感謝。整齊。 – user3677331