问题描述
我有两个承诺,一个拒绝,另一个解决。 Promise.all被称为。它执行了Promise.all的catch块,因为其中一个承诺被拒绝。
I have two promises, one rejected and other resolved. Promise.all is called. It executed the catch block of Promise.all as one of the promises is rejected.
const promise1 = Promise.resolve('Promise 1 Resolved');
const promise2 = Promise.reject('Promise 2 Rejected');
const promise3 = Promise.all([promise1, promise2])
.then(data => {
console.log('Promise.all Resolved', data);
})
.catch(error => {
console.log('Promise.all REJECTED', error);
})
setTimeout(() => {
console.log(promise1, promise2, promise3)
}, 200);
如果我没有Promise.all()的捕获,则值仍然为Rejected,即
If I don't have the catch on Promise.all(), the value remains as Rejected, ie
const promise3 = Promise.all([promise1, promise2])
.then(data => {
console.log('Promise.all Resolved', data);
})
我错过了有关承诺的内容。
Am I missing something about promises.
推荐答案
我看到它的答案,但我想我可以澄清一点。
I see that its answer but I think I can clarify a bit more.
请记住每个然后()
或 catch()
返回承诺
。 (如果在回调中没有任何显式 return
,则两者都将返回 Promise.resolve(undefined)
)。因此,在承诺解决后,整个承诺链的值将是最后然后()
返回的承诺;
示例:
Please remember that each then()
or catch()
return a Promise
. (If you don't have any explicit return
in callback, both will return Promise.resolve(undefined)
). Therefore after the promise has resolved, the value of entire promise chain will be the promise returned by last then()
;Example:
promise = Promise.resolve(1)
.then(() => Promise.resolve(2))
.then(() => Promise.resolve(3));
console.log(promise);
setTimeout(() => {
console.log(promise)//Promise {<resolved>: 3}
}, 0)
catch()
与完全相同然后()
。唯一的区别是它调用拒绝
承诺而不是已解决
。
在下面的例子中,我只需用 reject
替换所有 resolve
来证明这一点。
catch()
works in exactly like then()
. The only difference is that its called on rejected
promises rather then resolved
.In following example, I just replace all resolve
by reject
to demonstrate that.
promise = Promise.reject(1)
.catch(() => Promise.reject(2))
.catch(() => Promise.reject(3));
console.log(promise);
setTimeout(() => {
console.log(promise)//Promise {<rejectd>: 3}
}, 0)
现在回答你的问题。值 Promise.all()
是被拒绝的承诺,因为数组中的一个承诺被拒绝。如果你的链中有一个catch块,控制将转到 catch
块,它将返回 Promise.resolve(undefined)
。如果链中没有捕获块,您将得到您所拥有的:被拒绝的承诺。
Now coming to your question. Value of Promise.all()
is a rejected promise, since one of the promise in array is rejected. If you have a catch block in chain, control will go to that catch
block which will return a Promise.resolve(undefined)
. If you have no catch block in the chain, you will get what you have: a rejected promise.
这篇关于Promise.all()被拒绝后的值,显示[''PromiseStatus'']:如果存在catch块则解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!