2017-03-31 33 views
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已执行

回答

3

使问题行2只叫我相信你需要用外在的承诺来巩固内在的承诺,在这种情况下,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); 
       }); 
      }); 
    }); 
})); 
+1

哇,非常感谢。整齐。 – user3677331