Sobes.tech
Middle

// What will be printed to the console? const p1 = Promise.resolve(1); const p2 = new Promise((_, reject) => setTimeout(() => reject("Error"), 100)); const p3 = Promise.resolve(3); Promise.all([p1, p2, p3]) .then(console.log) .catch(console.error); Promise.allSettled([p1, p2, p3]).then(console.log);

sobes.tech AI

Answer from AI

The console will output the following:

  1. The call to Promise.all([p1, p2, p3]) will end with an error because p2 rejects after 100 ms with the message "Error". Therefore, in the .catch(console.error) block, the following will be printed:
Error
  1. The call to Promise.allSettled([p1, p2, p3]) always completes successfully, returning an array with the results of all promises, regardless of their state. The console will display an array of objects:
[
  { "status": "fulfilled", "value": 1 },
  { "status": "rejected", "reason": "Error" },
  { "status": "fulfilled", "value": 3 }
]

Thus, first an error from Promise.all appears in the console, followed by the array of results from Promise.allSettled.