Promises
suggest changeSyntax
new Promise((resolve, reject) => { /* asynchronous operation */ })promise.then(onFulfilled, onRejected)promise.catch(onRejected)promise.finally(onFinally)Promise.resolve(value)andPromise.reject(reason)Promise.all(iterable)andPromise.allSettled(iterable)Promise.race(iterable)andPromise.any(iterable)Promise.withResolvers()
Remarks
Promises have been standard since ECMAScript 2015 and are available in modern browsers and JavaScript runtimes. An async function always returns a promise, and await provides clearer syntax for many sequential workflows.
Promise reaction callbacks run asynchronously as microtasks, even when the promise is already settled. A promise represents one eventual outcome and does not itself cancel the underlying operation; APIs that support cancellation commonly accept an AbortSignal.
Choose the concurrency combinator that matches the required behavior:
Promise.all()fulfills with every result or rejects after the first rejection.Promise.allSettled()waits for every input and reports each outcome.Promise.any()fulfills with the first successful result or rejects withAggregateErrorwhen every input rejects.Promise.race()settles with the first input to settle, whether fulfilled or rejected.
Waiting for multiple concurrent promises
Reduce an array to chained promises
Waiting for the first of multiple concurrent promises
Promisifying functions with callbacks
Reconciling synchronous and asynchronous operations
Performing cleanup with finally