2017-03-31 66 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