Do not wrap async functions in new Promise
Do not wrap async functions in new Promise in JavaScript: an async executor loses its errors and the promise stays pending. Node.js 24.
An async function already returns a promise. Passing one to new Promise as the executor hides its errors, and the outer promise never settles.
const delay = (ms, v) => new Promise((r) => setTimeout(r, ms, v));
process.on('uncaughtException', (err, origin) => {
console.log('process saw', err.message, origin);
});
const bad = () =>
new Promise(async (resolve) => {
await delay(5);
throw new Error('lost');
});
const good = async () => {
await delay(5);
throw new Error('kept');
};
console.log(await Promise.race([bad(), delay(50, 'bad is still pending')]));
console.log(await good().catch((err) => err.message));process saw lost unhandledRejection
bad is still pending
keptThe constructor only catches errors thrown synchronously from the executor. An async executor returns its own promise, and when it throws after an await, that inner promise rejects with nobody listening. The outer promise is never resolved or rejected, so callers wait forever, and Node.js reports an unhandled rejection. The good version is the function itself, and its error reaches the caller.
ESLint has a rule for this, no-async-promise-executor. If you need resolve from an event, call it from a callback, not from an async executor.