Javascript 盎格鲁人承诺不会干净地离开
我有一个非常简单的承诺,它执行,我得到返回响应,在处理返回响应时,函数永远不会退出。我的测试已经变得越来越简单,只是把它简单化,但问题依然存在——我一辈子都不明白为什么要这样做Javascript 盎格鲁人承诺不会干净地离开,javascript,angularjs,httprequest,Javascript,Angularjs,Httprequest,我有一个非常简单的承诺,它执行,我得到返回响应,在处理返回响应时,函数永远不会退出。我的测试已经变得越来越简单,只是把它简单化,但问题依然存在——我一辈子都不明白为什么要这样做 isError = 0 ; // validating corporate ID if ($scope.installType == 1) { $scope.errMsg = "Validating Corporate Business ID" ; document.getElementById('errorMs
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
}
isError=0;
//正在验证公司ID
如果($scope.installType==1){
$scope.errMsg=“验证公司业务ID”;
document.getElementById('errorMsg').style.textDecoration='blink';
返回apiService.all()
.然后(功能(响应){
var corpData=响应[0];
if(corpData.rowCount==1&&corpData.data[0]。corpID==scope.userObj.corp\u ID){
//从理论上讲,找到了一个匹配项
console.log(“无错误”);
}否则{
//没有找到匹配项
console.log(“有错误”);
iSeries++;
}
log(“isError:+isError);//这将打印到控制台
//return;//添加了此选项,认为它已卡在承诺内,但仍然不起作用
}) ;
console.log(“hereA”);//该日志不会执行,因为它放在return语句之后
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语句之后
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;
在console.log之后(“isError:+isError”)但那也不起作用。我想如果没有返回
,它只会完成链并正常退出…当这不起作用时,我添加了返回来强制它,这也不起作用。我想说很可能是调用出错了。你只有一个success函数,因此在出现错误的情况下,似乎什么都不会发生发生。调用很好…这就是无错误
和有错误
正在做的事情-任何一个打印到控制台的事实都意味着承诺没有出错。如果您使用的是angular$http或$resource,这实际上不是他们正在做的事情。然后,只有在http调用成功通过时,才会通过。否则,将调用提供的第二个调用。示例:users.get()。然后(成功,错误);你说的和另一个人一样,但我不明白问题所在……当我意识到我看到的是错误的return
时,我能够用他的重写来处理问题。谢谢。嗯,我想是这样。但是为什么链根本不返回呢?我在console.log之后添加了一个return;
(“isError:”+isError)但那也不起作用。我想如果没有返回
,它只会完成链并正常退出…当这不起作用时,我添加了返回来强制它,这也不起作用。我想说很可能是调用出错了。你只有一个success函数,因此在出现错误的情况下,似乎什么都不会发生发生。调用很好…这就是无错误
和有错误
正在做的事情-任何一个打印到控制台的事实都意味着承诺没有出错。如果您使用的是angular$http或$resource,这实际上不是他们正在做的事情。然后,只有在http调用成功通过时,才会通过。否则,将调用提供的第二个调用。示例:users.get()。然后(成功,错误);你说的和另一个人一样,但我不明白问题所在……当我意识到我看到的是错误的return
时,我能够让他的重写工作正常进行。谢谢。承诺链中唯一的return已经被注释掉了。如果($scope.installType…)
。无论是否使用返回
,结果都是一样的……它永远不会到达hereA
-这意味着上面的整个代码所属的更大的函数永远不会执行。不,您注释掉的返回在然后
中,并且不会改变控制台.log('hereA')的事实
从不执行,它们属于不同的上下文。尝试执行我现在更新的代码,它会记录这里a
啊…我跟你说了。我甚至没有想过返回,我的重点是更深入。现在测试。承诺链中唯一的返回已被注释掉。如果($scope.installType…)。无论是否使用返回
,结果都是一样的……它永远不会到达hereA
-这意味着上面的整个代码所属的更大的函数永远不会执行。不,您注释掉的返回在然后
中,并且不会改变控制台.log('hereA')的事实
从不执行,它们属于不同的上下文。请尝试执行我现在更新的代码,它会记录此处a
啊……我跟在你后面。我甚至没有想到返回,我的重点是更深入的。现在测试。