Consider the following assignments:
const promise1 = (async () => {
await someAsyncFunction();
})();
const promise2 = new Promise(async resolve => {
await someAsyncFunction();
resolve();
});
Is there any meaningful difference between these two Promises (other than verbosity)?
2
Answers
Consider the following definition of
someAsyncFunction
:In this case, both promise 1 and promise 2 cause an unhandled rejection error. But afterwards, the two promises are in different states. Promise 1 (defined using the async IIFE) has rejected with the reason "Error in someAsyncFunction!" but promise 2 is still pending. This behavior was confirmed in NodeJS and Firefox.
They’re basically the same, the
IIFE
is syntactic sugar for the 2nd version