0
votes
  1. The documentation for promises states that .catch()returns a promise. Does this mean that if you return a promise in your code it will get wrapped in another promise from the .catch?

i.e.

const x = Promise.reject().catch(() => Promise.resolve("test"));

Will the above promise that resolves with the value of "test" be wrapped in another promise from the .catch? To my understanding this is what happens in async functions; they wrap whatever the result is in side of a promise "under the hood". Which could then result in double promises, is that also happening here?

  1. If the promise in the variable x above was put into an existing array and passed to Promise.all(), what would constitute as being resolved? Would the catch block and the inner function have to complete before Promise.all resolves? If so, why is this the case? Why would Promise.all not resolve as soon as the first Promise.reject() is executed? How would it know to wait?
1

1 Answers

1
votes
  1. Yes and no ... there are multiple promises involved, but they are chained, not wrapped in each other, or in the final promise "takes on" (the actual phrase is "adopts") the value of the Promise.resolve("test") in your case, so, what you get is a single promise that settles (in this case, resolves) to the value "test"
  2. The 5 Answers are:
  • yes, it is resolved,
  • yes, because x is the final promise returned by that expression
  • because x is the final promise returned by that expression, none of the other Promises are "visible" to the Promise.all,
  • because x is the final promise returned by that expression, the other Promises are not "visible" to x
  • that's how promise chains work, there's no waiting, it's just promise chaining at work

You may find The Promise Resolution Procedure - 2.3.2 helps in understanding the inner workings of Promises - 2.3.2 specifically deals with returning a Promise inside .then ... the rest of that resolution procedure is also illuminating.