Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS链接承诺-需要在下一个';然后';_Angularjs_Angular Promise - Fatal编程技术网

AngularJS链接承诺-需要在下一个';然后';

AngularJS链接承诺-需要在下一个';然后';,angularjs,angular-promise,Angularjs,Angular Promise,我在一个承诺链上工作。第一个调用是一个$http调用,用于检查用户是否存在,如果存在,则有一组按顺序运行的.then()语句 我的问题是。。在第一个调用中,我不想返回$http请求的承诺,因为如果用户不存在,结果只是一个空数组,承诺将被解析,从而触发下一个查找用户信息的操作。我写了以下代码 (参见评论中关于成为我所问的重要部分的部分) $scope.checkIfUserExists=function(){ if(角度对象($scope.admin.Inductee.Contactor)){ v

我在一个承诺链上工作。第一个调用是一个$http调用,用于检查用户是否存在,如果存在,则有一组按顺序运行的.then()语句

我的问题是。。在第一个调用中,我不想返回$http请求的承诺,因为如果用户不存在,结果只是一个空数组,承诺将被解析,从而触发下一个查找用户信息的操作。我写了以下代码

(参见评论中关于成为我所问的重要部分的部分)

$scope.checkIfUserExists=function(){
if(角度对象($scope.admin.Inductee.Contactor)){
var handleFault=功能(故障){
if(类型(故障)=='string'){
开关(fault.toUpperCase()){
案例“NODATA”:
//进行一次扑救
$scope.pushInductee();
打破
案例“状态”:
//只要在那里签下“重复记录检查”就行了
//“保存”按钮被严重错误禁用
$scope.hideSave=false;
打破
“指定”案例:
//只要在那里签下“重复记录检查”就行了
//“保存”按钮被严重错误禁用
$scope.hideSave=true;
打破
违约:
$log.error(故障);
$location.path('/error/default');
}
}否则{
$log.error(故障);
$location.path('/error/default');
}
};
$scope.getMatchingAddata()
.then($scope.procBusLogic)
.then($scope.pushy)
.接住(把手);
} 
};
////这是我要问的重要部分
$scope.getMatchingIData=function(){
var deferred=$q.deferred();
变量局部变量={};
var checkUser=函数(dupeJson){
var checkUserDeferred=$q.deferred();
//放弃的承诺换成了我自己的承诺
sttiJoinDataFactory.checkIfUserExistsNurseleader(dupeJson)
.然后(函数(结果){
var数据=结果数据;
如果(角度isArray(数据)和数据长度>0){
var highestMatch=数据[0];
对于(变量i=0;iparseInt(highestMatch.Score)){
最高匹配=数据[i];
}
}
checkUserDeferred.resolve(最高级匹配);
}否则{
//在这里拒绝“整体”承诺
//有效地打破链条
延迟返回。拒绝(“NODATA”);
}
})
.catch(功能(故障){
//任何其他故障都会导致链条断裂
//此时的http请求数
返回延迟。拒绝(错误);
});
返回checkUserDeferred.promise;
},
loadindividual=函数(最匹配){
返回有关最高级匹配的$http内容
//用局部变量设置数据
},
ParallelLoadStatus和InducteData=函数(单个){
基于上一个then()返回另一个$http承诺
//用局部变量设置数据
},
loadCeremonyData=功能(感应式){
基于最后一次调用返回另一个$http承诺,然后()//在本地设置数据
},
报告问题=功能(故障){
拒绝(过失);
};
checkUser($scope.generateDupJson())
.然后(加载个人,报告问题)
.然后(并行加载状态和行业数据,报告问题)
.然后(加载CeremonyData,报告问题)
.then(函数(){
延迟。解决(本地);
})
.捕获(报告问题);
回报。承诺;
};
我必须考虑被放弃的承诺,因为我确实需要承诺在数据返回时解决问题,如果有NODATA,我需要拒绝它。这是在调用函数的链中处理的


另外,我知道这里有反模式。我正在尽我最大的努力不去做承诺、维护链条以及处理例外情况。

好的,我有几点意见要告诉你:

 ...
 // revert if and return immediately
 // to reduce indentation
 if (typeof(fault) !== 'string') {
   $log.error(fault);
   $location.path('/error/default');
   return;
 }
 switch (fault.toUpperCase()) {
 ...
您不需要延迟对象:

var checkUser = function(dupeJson){

  // this is not abandoned because we are returning it
  return sttiJoinDataFactory.checkIfUserExistsNurseleader(dupeJson)
  .then(function(results) {
    var data = results.data;
    if (!angular.isArray(data) || data.length <= 0) {
      return $q.reject('NODATA');
    }

    var highestMatch = data.reduce(function (highest, d) {
      return parseInt(d.Score) > parseInt(highest.Score) ? 
        d : highest;
    }, data[0]);

    return highestMatch;
  }); // you don't need catch here if you're gonna reject it again
}

...

checkUser(...)
  // loadIndividual will be called
  // after everything inside checkUser resolves
  // so you will have your highestMatch
  .then(loadIndividual)
  .then(parallelLoadStatusAndInducteeData)
  .then(loadCeremonyData)
  // you don't need to repeat reportProblems, just catch in the end
  // if anything rejects prior to this point
  // reportProblems will be called
  .catch(reportProblems)
  ...
var checkUser=function(dupeJson){
//这并不是因为我们要归还而放弃的
返回sttiJoinDataFactory.checkIfUserExistsNurseleader(dupeJson)
.然后(函数(结果){
var数据=结果数据;
如果(!angular.isArray(数据)| data.length parseInt(最高.Score)?
d:最高;
},数据[0]);
返回最高匹配;
}); // 如果你想再次拒绝它,你不需要在这里抓住它
}
...
checkUser(…)
//将调用loadIndividual
//在checkUser中的所有内容解决后
//那么你将拥有最崇高的荣誉
.然后(个人)
.然后(并行加载状态和行业数据)
.然后(加载Ceremonydata)
//你不需要重复报告问题,只需要在最后抓住问题
//如果在此之前有任何拒绝
//报告问题将被调用
.catch(报告问题)
...

我喜欢这种方法。不过,我想知道$q.reject是如何与sttiJoinDataFactory的$http承诺“交互”的。如果代码已经在.then()节中,我们是否已经从技术上解决了“成功”问题?我实现了这个,并且正如我所想的,它在拒绝后继续沿着链运行,运行loadindividual、ParallelLoadStatusAndInducteData和LoadCeremonyData如果您返回nothing和链a。然后关闭nothing,您将得到一个错误,因为undefined.then()实际上不是一个方法。@AmyBlankenship这是针对谁的?我返回了一个延迟承诺,但是孤立了$http承诺,因为它没有用。你不是孤立它,你的延迟对象和$http承诺之间有语义绑定。
var checkUser = function(dupeJson){

  // this is not abandoned because we are returning it
  return sttiJoinDataFactory.checkIfUserExistsNurseleader(dupeJson)
  .then(function(results) {
    var data = results.data;
    if (!angular.isArray(data) || data.length <= 0) {
      return $q.reject('NODATA');
    }

    var highestMatch = data.reduce(function (highest, d) {
      return parseInt(d.Score) > parseInt(highest.Score) ? 
        d : highest;
    }, data[0]);

    return highestMatch;
  }); // you don't need catch here if you're gonna reject it again
}

...

checkUser(...)
  // loadIndividual will be called
  // after everything inside checkUser resolves
  // so you will have your highestMatch
  .then(loadIndividual)
  .then(parallelLoadStatusAndInducteeData)
  .then(loadCeremonyData)
  // you don't need to repeat reportProblems, just catch in the end
  // if anything rejects prior to this point
  // reportProblems will be called
  .catch(reportProblems)
  ...