javascript - Angular then() 内的多个函数

标签 javascript angularjs promise angular-promise

有时我在 AngularJs 的 promise then() 内部看到两个以上的函数被逗号分隔。有谁能帮忙解释一下这个结构是什么意思吗?

例如

then(function(response){..}, function(response){..}, function(response){..}, function(response){..});

我的理解是,如果 then() 中有两个函数。如果第一个函数满足 promise ,它将运行,否则如果发生任何错误,第二个函数将运行。这个结构看起来也不像链式 promise ...

非常感谢您在这里提供的任何帮助:-)

最佳答案

嗯:

  • 第一个函数是fulfillment处理程序,它会在 promise 解析为一个值(通常)时执行。
  • 第二个函数是rejection 处理程序,它会在 promise 通过拒绝(或在处理程序中抛出异常)解决错误时执行。
  • 第三个函数用于progress,这是一个非标准且已弃用的功能,永远不会成为 ECMAScript promise 的一部分 - 最好完全避免使用它,因为它组合不好。 Angular 的新 $q API 也不支持它。
  • 传入的第三个函数之后的任何内容都会被处理程序忽略,并且不会产生任何影响。

以下是所有三个被调用的示例:

var good = $q.when(3);
good.then(x => console.log("Successful"));
var bad = $q.reject(Error("Bad")); // always reject with errors
bad.then(null, x => console.log("Failed"));
var d = $q.defer(); // don't do this like... ever
d.promise.then(null, null, x => console.log("Progressed"));
d.notify("event notification"); // never use this in real code ;)

关于javascript - Angular then() 内的多个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33130714/

相关文章:

javascript - 以 Promise.all 为条件

javascript - 对象捕捉到 Fabric.js 上的自定义网格

javascript - AngularJS 服务根作用域

php - 从字节数组输出 PHP 中的 PDF

javascript - 使用 angularjs , nodejs , expressjs 单击按钮后在前端下载 Csv 文件

javascript - 在隐藏元素中绑定(bind)日期选择器

javascript - 外部函数调用、promise、async 和 Mongo - 困惑

javascript - 如何使用 Bluebird promise Node 的 child_process.exec 和 child_process.execFile 函数?

javascript - 如何在页面加载时加载隐藏div的JS内容

javascript - 使用 AJAX 根据 Codeigniter 中的下拉值获取数据库值