有时我在 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/