【问题标题】:AngularJS : Adding variable into a chain of promisesAngularJS:将变量添加到承诺链中
【发布时间】:2015-09-25 13:01:44
【问题描述】:

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

在这种情况下正在运行三个函数。

//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');
});

【问题讨论】:

  • 你的意思可能是return exerciseSearch(result, term)

标签: javascript angularjs asynchronous promise angular-promise


【解决方案1】:

Promise 链应该始终有来自 .then 的返回对象以继续 Promise 链

//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');
});

【讨论】:

  • 您确定您传递的result 是来自正确搜索的那个吗?
  • @Bergi 你是对的..他应该返回在exerciseSearch中创建的object..谢谢提醒..让我更新一个答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 2013-09-16
  • 1970-01-01
  • 2018-06-05
  • 1970-01-01
相关资源
最近更新 更多