2017-03-31 4 views
0

私はforEachループですべての約束が完了するまで待つ必要がある状況にあります。入れ子のレベルがちょうど深い場合は、公園内を散歩していたでしょう。私の場合、私は約束の中で約束を待ってからq.allSettledに移動しなければなりません。ラフなコードは以下のとおりである:q.all入れ子のasync 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(); 
     }); 
    }); 

私はちょうど問題1号線

答えて

3

を実行した後に問題2号線のみ呼び出されるようにループのために上記のを待つするかどうかはわかりません私は外側の約束で内側の約束を縛る必要があると信じています。この場合、dimsAndFeePromisematchingPromisesと結婚する必要があります。

以下のコードあなたは正しい方向に取得する必要があります。

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

関連する問題