Skip to content

Files

Latest commit

 

History

History
29 lines (23 loc) · 797 Bytes

no-single-promise-in-promise-methods.md

File metadata and controls

29 lines (23 loc) · 797 Bytes

Pattern: Single promise in Promise method array

Issue: -

Description

Using Promise.all(), Promise.any(), or Promise.race() with a single-element array is unnecessary and likely a mistake. Use await directly on the promise or Promise.resolve() for non-promises.

Examples

Example of incorrect code:

async function bad() {
  const foo = await Promise.all([promise]);
  const foo = await Promise.any([promise]);
  const foo = await Promise.race([promise]);
  const promise = Promise.all([nonPromise]);
}

Example of correct code:

async function good() {
  const foo = await promise;
  const promise = Promise.resolve(nonPromise);
  const foo = await Promise.all(promises);
  const foo = await Promise.any([promise, anotherPromise]);
}