Here is a wierd behaviour that’s been bugging me for a whole day. All the usual docs and even AI explanations don’t make sense - so i have a feeling this might be something actually explained in the spec.
This throws synchronous error -
const taskFn = () => {
throw new Error('sync error');
};
const {promise, resolve, reject} = Promise.withResolvers();
promise.catch(() => {});
const taskPromise = Promise.resolve().then(() => taskFn());
taskPromise
.then(resolve, reject);
taskPromise
.finally(() => {
});
while this doesn’t -
const taskFn = () => {
throw new Error('sync error');
};
const {promise, resolve, reject} = Promise.withResolvers();
promise.catch(() => {});
const taskPromise = Promise.resolve().then(() => taskFn());
taskPromise
.then(resolve, reject)
.finally(() => {
});
The only difference is that second one has finally chained after then.
Why does this matter, since finally doesn’t handle any error?
(Sorry if it’s a very basic question)