0
  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?
pzero1
  • 103
  • 1
  • 8

1 Answers1

1
  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.

Bravo
  • 6,022
  • 1
  • 10
  • 15
  • Thanks for your response. For 1) I am more wondering about the .catch() specifically. For example if it had been instead: var x = Promise.reject().catch(() => "test"); Would this mean that the catch would know to wrap the string "test" in a promise to make sure x is in fact a promise and not a string? Thus, also in my question above does the Promise.resolve("test") in the .catch also get wrapped in another promise? – pzero1 Nov 15 '20 at 23:59
  • 1
    .then and .catch always return a Promise, so, `var x = Promise.reject().catch(() => "test");` is identical result to `const x = Promise.reject().catch(() => Promise.resolve("test"));` ... as far as the Promise that `x` resolves to - and there is no "wrapping" of Promises ... if the code inside `.then/.catch` returns a Promise, the promise returned by `.then/.catch` adopts the state of the Promise returned ... read [The Promise Resolution Procedure - 2.3.2](https://promisesaplus.com/#point-49) - in fact, read the whole thing to understand the inner workings of Promises – Bravo Nov 16 '20 at 00:02
  • You may notice the Promise Resolution procedure makes no mention of `.catch` ... because `.catch` is just syntax sugar for `.then(null, onRejectFunction)` – Bravo Nov 16 '20 at 00:07