javascript - AngularJS : Adding variable into a chain of promises

标签 javascript angularjs asynchronous promise angular-promise

我正在学习 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');
});

最佳答案

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

关于javascript - AngularJS : Adding variable into a chain of promises,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31278764/

相关文章:

javascript - 如何将对象数组分配给AngularJS中的下拉列表?

node.js - 单线程同步和异步混淆

javascript - Highcharts 列区域

javascript - 通过纯JavaScript验证比特币地址(不是node.js)

asp.net-mvc - 从 Web api + AngularJS 中的 JSON 中删除 ↵

php - 使用ajax成功提交表单后重新加载页面

python - 如何将协程添加到正在运行的 asyncio 循环中?

javascript - 在现有数组的中间插入一个数组。保持现有引用不变

javascript - 在更改下拉菜单时更新露天工作流程中的表单

javascript - 在 Angular 中广播站点范围的错误消息