gpt4 book ai didi

javascript - 处理已解决/已拒绝的 Angular promise

转载 作者:行者123 更新时间:2023-11-28 19:00:37 25 4
gpt4 key购买 nike

这两种处理已解决/已拒绝的 Angular Promise 的方法是否等效?

附件A

promise.then(function (value) {
console.log('promise resolved with val', value);
return 'resolved';
})
.catch(function (reason) {
console.log('promise rejected with reason', reason);
return 'rejected';
})

附件 B

promise.then(function (value) {
console.log('promise resolved with val', value);
return 'resolved';

}, function (reason) {
console.log('promise rejected with reason', reason);
return 'rejected';
});

我怀疑的原因是因为我读到then返回一个新的promise,所以看起来A在功能上与

var anotherPromise = promise.then(function (value) {
console.log('promise resolved with val', value);
return 'resolved';
});

anotherPromise.catch(function (reason) {
console.log('promise rejected with reason', reason);
return 'rejected';
})

它看起来与 B 不同,因为 catch 正在处理 anotherPromise 的拒绝,而不是 promise

最佳答案

Promises 使用方法链返回相同的 Promise 实例,因此即使看起来您正在设置的版本中创建 Promise 的新版本..

var anotherPromise = promise.then(function (value) {

..promise 和 anotherPromise 都指向同一个对象。

同样的情况适用于:

var a = {};
var b = a;
a.monkey = 'banana';
console.log(b.monkey); // 'banana'

关于javascript - 处理已解决/已拒绝的 Angular promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32637996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com