简体   繁体   English

多个 javascript promises.then and.catch 打错了

[英]Multiple javascript promises .then and .catch hitting wrong catch

I think that I am not understanding something properly here as it's very strange behaviour.我认为我在这里没有正确理解某些东西,因为这是非常奇怪的行为。 If I call queryFindPlayer it should be falling into the.then which it does if queryFindContract function is not there but when it is there like below it seems to fall to the queryFindPlayer catch and add a new player.如果我调用 queryFindPlayer,它应该落入 the.then 中,如果 queryFindContract function 不存在,但当它出现在下面时,它似乎落入 queryFindPlayer 捕获并添加新玩家。

queryFindPlayer(models, ConsoleId, UserId, SeasonId, LeagueId).then(players => {
    const player = players[0];
    queryFindContract(db, player.Team.id, UserId, SeasonId, LeagueId).then(contracts => {
      console.log("player has a contract to a team");
    }).catch(e => {
      console.log("failed to find player");
    });
}).catch(e => {
  queryAddPlayer(models, UserId, TeamId).then(player => {
    console.log("added player");   
  }).catch(addPlayerError => {
    console.log("failed to add player, shouldn't happen");
  });          
});

If queryfindPlayer() resolves so you start execution of the .then() handler, but then you end up in the queryFindPlayer().catch() handler, that can occur for one of the following reasons:如果queryfindPlayer()解析,因此您开始执行 .then( .then()处理程序,但最终进入queryFindPlayer().catch()处理程序,这可能由于以下原因之一发生:

  1. If your code threw an exception before calling queryFindContract() such as if const player = players[0] threw an error of if queryFindContract() wasn't defined.如果您的代码在调用queryFindContract()之前抛出异常,例如 if const player = players[0]抛出 if queryFindContract()未定义的错误。

  2. If your code threw an exception evaluating the arguments to pass queryFindContract() such as player.Team.id throws or any of the other variables you're passing don't exist.如果您的代码抛出异常,评估 arguments 以传递queryFindContract() ,例如player.Team.id抛出或您传递的任何其他变量不存在。

  3. If queryFindContract() throws synchronously before it returns its promise.如果queryFindContract()在返回 promise 之前同步抛出。

  4. If queryFindContract() doesn't return a promise and thus queryfindContract().then() would throw an exception.如果queryFindContract()不返回 promise 并且因此queryfindContract().then()将引发异常。

All of these will cause a synchronous exception to be thrown in the queryFindPlayer.then() handler which will cause it to go to the queryFindPlayer.catch() handler.所有这些都将导致在queryFindPlayer.then()处理程序中引发同步异常,这将导致它到 go 到queryFindPlayer.catch()处理程序。 It never gets to the queryFindContract().catch() handler because queryFindContract() either never got to execute or because it never got to finish and return its promise.它永远不会到达queryFindContract().catch()处理程序,因为queryFindContract()永远不会执行,或者因为它永远不会完成并返回其 promise。


You can most likely see exactly what is causing your situation by just adding您很可能只需添加即可准确了解导致您的情况的原因

console.log(e)

at the start of both .catch() handlers.在两个.catch()处理程序的开头。 For clarity, also add a descriptive string before the e .为清楚起见,还要在e之前添加一个描述性字符串。 such as:如:

console.log("qfc", e);

and

console.log("qfp", e);

I pretty much always log rejections, even if expecting them sometimes because you can also get rejections for unexpected reasons such as programming errors and you want to be able to see those immediately and not get confused by them.我几乎总是记录拒绝,即使有时会期待它们,因为您也可能因意外原因(例如编程错误)而被拒绝,并且您希望能够立即看到这些拒绝而不会被它们弄糊涂。

Thanks for the help, I was not using the exception handler how intended.感谢您的帮助,我没有按预期使用异常处理程序。

queryGetContractById(models, id).then(c => {
  return queryFindPlayer(models, ConsoleId, UserId, SeasonId, LeagueId).then(players => {
    if(players.length != 0) {
      return queryFindContract(models, players[0].Team.id, UserId, SeasonId, LeagueId).then(contracts => {
        if(contracts.length != 0) {
          console.log("player has a contract to a team");
        } else {
          queryUpdatePlayersTeam(models, players[0].id, TeamId);
        }
      });
    } else {
      return queryAddPlayer(models, UserId, TeamId).then(player => {
        console.log("added player");   
      });
    }
  });
}).catch(e => {
  console.log("failed", e);   
});

In my promise, I rejected if there was no data returned and resolving if there was.在我的 promise 中,如果没有返回数据我拒绝,如果有则解决。 I can see how this was wrong and I think this is now right.我可以看到这是错误的,我认为现在是正确的。

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

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