Sobes.tech
Middle

Determine the order of output of digits in the console for the Promise example where reject is called, and then handlers then, catch, finally are attached.

sobes.tech AI

Answer from AI

If a Promise is immediately invoked with reject, and then handlers then, catch, finally are attached, the output order will be as follows:

const p = new Promise((resolve, reject) => {
  reject('Error');
});

p.then(() => console.log('then'))
 .catch(() => console.log('catch'))
 .finally(() => console.log('finally'));

The console will display:

catch
finally

Explanation:

  • When reject is called, the promise transitions to the rejected state.
  • then with the first argument for a successful result is skipped.
  • catch is triggered and logs 'catch'.
  • finally always executes, regardless of the result, and logs 'finally'.

Handlers then/catch/finally are called asynchronously after the current stack, so they will execute in the order of the microtask queue.

Determine the order of output of digits in the… - sobes.tech