简体   繁体   English

承诺链未按预期顺序执行-Node.js

[英]Promise chain not executing in expected order - nodejs

I have a chain of 4 promises, and 1 function at the end. 我有一个由4个诺言组成的链,最后有1个职能。 The final function is executing before the previous promises in the chain have resolved. 最终功能在链中先前的承诺解决之前执行。

Can someone explain to me why this might be happening? 有人可以向我解释为什么会这样吗?

Here is the promise chain: 这是承诺链:

 updateGdax(db) .then(updateBitstamp(db)) .then(updateBitfinex(db)) .then(updatePoloniex(db)) .then(coinMarketData.updateCoinMarketData(db)) .then(addRates(db)); //this function is executing after the first promise in the chain. 

I would like each function to execute after the one listed before it, so addRates(db) should be executed last. 我希望每个函数在其列出的每个函数之后执行,因此addRates(db)应该最后执行。

I can post the code from the promise functions if needed for further analyses, but I really just want to understand WHY this would happen, as my understanding is that functions in a promise chain won't execute unless the previous promise in the chain has resolved. 如果需要进一步分析,我可以从promise函数中发布代码,但我真的只想了解为什么会发生这种情况,因为我的理解是,除非链中的先前promise已解决,否则promise链中的函数将不会执行。 。

Unless those update functions in the then calls are partially applied (unless they return a function), they are being executed before the then is called. 除非部分应用了then调用中的那些更新函数(除非它们返回一个函数),否则它们将在then调用之前被执行。 You need to wrap them in an anonymous function to have them executed in order. 您需要将它们包装在一个匿名函数中,以便按顺序执行它们。 Do what the other answer says or use fat arrows: 按照其他答案说的做,或使用粗箭头:

updateGdax(db)
  .then(()=>updateBitstamp(db))
  .then(()=>updateBitfinex(db))
  .then(()=>updatePoloniex(db))
  .then(()=>coinMarketData.updateCoinMarketData(db))
  .then(()=>addRates(db)); 

If your update functions could be rewritten to return the db after completing, then you could rewrite the calls like so, point free style: 如果您的更新功能可以在完成后重写为返回db,那么您可以这样重写调用,即指向自由样式:

updateGdax(db)
  .then(updateBitstamp)
  .then(updateBitfinex)
  .then(updatePoloniex)
  .then(coinMarketData.updateCoinMarketData)
  .then(addRates); 

Each function, would then look something like this: 每个函数将如下所示:

function updateGdax(db) {
   return db.doSomething().then(()=> db)
}

Follow that pattern, and you have yourself some nice looking javascript. 按照这种模式,您将拥有一些漂亮的javascript。

And have a look at the new async/await, included in nodejs 8. It is much more intuitive: 并查看一下nodejs 8中包含的新async / await。它更加直观:

async function main() {
  await updateGdax(db)
  await updateBitstamp(db)
  await updateBitfinex(db)
  await updatePoloniex(db)
  await coinMarketData.updateCoinMarketData(db)
  await addRates(db)
}

main().catch(e => console.error(e))

Try below approach, 尝试以下方法,

updateGdax(db)
  .then(function(){
    return updateBitstamp(db)
  }).then(function (){
    return updateBitfinex(db);
  }).then(function() {
    return updatePoloniex(db);
  }).then(function(){
    return coinMarketData.updateCoinMarketData(db)
  }).then(function(){
    return addRates(db);
  }).catch(function(err){
    console.log(err);
  });

Hope this will work. 希望这会起作用。 If any of the function is returning any value and if you want to use it in subsequent function the pass that value in following function() used inside then. 如果任何函数返回任何值,并且要在后续函数中使用该值,则在内部使用的以下function()中传递该值。 Refer : https://strongloop.com/strongblog/promises-in-node-js-an-alternative-to-callbacks/ 请参阅: https : //strongloop.com/strongblog/promises-in-node-js-an-alternative-to-callbacks/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM