angular then() 中的多个函数

multiple functions inside of angular then()

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

例如

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

我明白了,如果then()里面有两个函数。如果第一个函数实现了承诺,它将 运行 ,否则如果发生任何错误,第二个函数将 运行 。这个结构看起来也不像链式承诺...

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

嗯:

  • 第一个函数是 fulfillment 处理程序,它将在 promise 解析为一个值(通常)时执行。
  • 第二个函数是 rejection 处理程序,它将在 promise 通过拒绝(或在处理程序中抛出异常)解决错误时执行。
  • 第三个函数用于 progress,这是一个非标准且已弃用的功能,永远不会成为 ECMAScript 承诺的一部分 - 最好完全避免它,因为它不会作好。 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 ;)