AngularJS:将变量添加到承诺链中

时间:2015-07-07 20:35:50

标签: javascript angularjs asynchronous promise angular-promise

我正在学习承诺并且正在努力解决以下问题。

在这种情况下正在运行三个功能。

//returns an search query in JSON format
filterSearch() 

  // searches Parse.com and returns an array of values
  .then(performSearch)

  // I am passing the array of values to exerciseSearch and a another value (term - string)
  .then(function(result) {
     exerciseSearch(result, term);
     })

  // The results from the search is displayed in scope. 
  .then(function(exercises) {
     $scope.allExercises = exercises;

  }, function(error) {
     console.log('error');
});

1 个答案:

答案 0 :(得分:3)

Promise chain应始终具有.then的返回对象以继续承诺链

//returns an search query in JSON format
filterSearch() 

  // searches Parse.com and returns an array of values
  .then(performSearch)

  //(term - string)
  .then(function(result) {
       return exerciseSearch(result, term); //exerciseSearch should return exercises from fn
     })

  // The results from the search is displayed in scope. 
  .then(function(exercises) {
     $scope.allExercises = exercises;
     return exercises;
  }, function(error) {
     console.log('error');
});
相关问题