If a Promise p
is resolved with the value of a Promise (or Thenable) q
, it essentially becomes a copy of Promise q
. If q
is resolved, p
will be resolved with the same value.
Promise.resolve(Promise.resolve("hello"));
Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "hello"}
If q
is rejected, p
will be rejected with the same value.
Promise.resolve(Promise.reject(new Error("goodbye")));
Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: Error: goodbye}
The fact that Promise p
was resolved/rejected through Promise q
, instead of directly with the respective value, is irrelevant to the final result. The intermediate Promise is consumed as part of the resolution process, and is not visible to the consumer.
If q
is a never resolved or rejected, p
will also remain pending forever.
Promise.resolve(new Promise(() => null)); // perpetually-pending promise
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
These cases are well-known, but I have never seen what happens if a Promise is rejected (instead of resolved) with another Promise value. Does the rejection process also consume intermediate Promises, or are they passed through intact?
If it does consume them, how does that work?