我的情况是,在继续之前,我必须等待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
我相信你需要用外部承诺链接内部承诺,在这种情况下,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);
});
});
});
}));