【问题标题】:AngluarJS promise not exiting cleanlyAngularJS 承诺不会干净地退出
【发布时间】:2017-03-24 18:08:03
【问题描述】:

我有一个非常简单的承诺,它执行,我得到返回响应,在处理返回响应时,就像函数永远不会退出一样。我的测试变得越来越简单,只是将其归结为基础,但问题仍然存在 - 我一生都无法理解为什么要这样做。

isError = 0 ;
// validating corporate ID
if ($scope.installType == 1) {
  $scope.errMsg = "Validating Corporate Business ID" ;
  document.getElementById('errorMsg').style.textDecoration = 'blink' ;

  return apiService.all()
  .then(function(response){
    var corpData = response[0] ;
    if (corpData.rowCount == 1 && corpData.data[0].corpID == $scope.userObj.corp_ID) {
      // theoretically a match was found
      console.log("no error") ;
    } else {
      // no match was found
      console.log("with error") ;
      isError++ ;
    }
    console.log("isError: "+isError) ;  // this prints to console
    //return ;  // had added this thinking it was stuck inside the promise, still didn't work
  }) ;
  console.log("hereA") ;  // <-- never gets here
}

【问题讨论】:

    标签: javascript angularjs httprequest


    【解决方案1】:

    该日志没有执行,因为它被放置在返回语句之后。

    isError = 0 ;
    // validating corporate ID
    if ($scope.installType == 1) {
      $scope.errMsg = "Validating Corporate Business ID" ;
      document.getElementById('errorMsg').style.textDecoration = 'blink' ;
    
      return apiService.all() // <= watch this return statement
          .then(function(response){
            var corpData = response[0] ;
              if (corpData.rowCount == 1 && corpData.data[0].corpID == $scope.userObj.corp_ID) {
              // theoretically a match was found
               console.log("no error") ;
            } else {
               // no match was found
              console.log("with error") ;
              isError++ ;
            }
            console.log("isError: "+isError) ;  // this prints to console
            //return ;  // had added this thinking it was stuck inside the promise, still didn't work
          }) ;
      console.log("hereA") ;  // <-- Code after a return never executes
    }
    

    把它放在return之前的任何地方,它就会按原样执行。我已经重新缩进了你的代码并评论了它,希望它更容易理解。

    为避免将来出现此类问题,请考虑安装 Eslint 之类的东西并将其集成到您的 IDE/编辑器中。 Its no-unreachable rule 会立即警告您您面临的问题。

    编辑:

    if ($scope.installType == 1) {
      $scope.errMsg = "Validating Corporate Business ID" ;
      document.getElementById('errorMsg').style.textDecoration = 'blink' ;
    
       apiService.all() // <= removed return statement
          .then(function(response){
            var corpData = response[0] ;
              if (corpData.rowCount == 1 && corpData.data[0].corpID == $scope.userObj.corp_ID) {
              // theoretically a match was found
               console.log("no error") ;
            } else {
               // no match was found
              console.log("with error") ;
              isError++ ;
            }
            console.log("isError: "+isError) ;  // this prints to console
            //return ;  // had added this thinking it was stuck inside the promise, still didn't work
          }) ;
      console.log("hereA") ;  // <-- this executes
    }
    

    【讨论】:

    • 该承诺链中唯一的回报已被注释掉。它应该刚刚退出到原来的if ($scope.installType...)。不管有没有return,结果都是一样的……它永远不会到达hereA——这意味着更大的函数是整个上述代码的一部分永远不会被执行。
    • 不,您注释掉的返回值在then 内部,并且不会改变console.log('hereA') 从不执行的事实,它们属于不同的上下文。尝试执行我现在更新的代码,它会记录hereA
    • 啊...我跟着你。我什至没有想到那个回报,我的注意力进一步下降。立即测试。
    【解决方案2】:

    IT 永远无法到达那里,因为控制台日志在您构建的函数链之外,并且在返回之后发生。

    【讨论】:

    • 好吧,我想。但是为什么它根本没有返回链条呢?我在 console.log("isError: "+isError) 之后添加了 return ; ,但这也不起作用。我想如果没有return,它只会完成链条并正常退出......当它不起作用时,我添加了 return 来强制它,但它也不起作用。
    • 我会说很可能是调用出错了。你只有一个成功函数,所以在出现错误的情况下似乎什么都不会发生。
    • 调用没问题...这就是no errorwith error 正在做的事情——任何一个打印到控制台的事实都意味着promise 没有出错。
    • 如果您使用 angular $http 或 $resource,这实际上不是他们正在做的事情。 .then 只有在您的 HTTP 调用成功通过时才会通过。否则,调用提供的第二个调用。示例:users.get().then(success, error);
    • 你说的和其他人一样,但我不明白这个问题......一旦我意识到我看错了return,我就可以用他的重写来解决问题.谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多