Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@emmveqz/js-utils

emmveqz72ISC1.0.3TypeScript support: included

Utilities for JavaScript

js, js-lib, js-library, js-tools, js-utils, ts-lib, ts-library, ts-tools, ts-utils, javascript, javascript-lib, javascript-library, javascript-tools, javascript-utils, typescript-lib, typescript-library, typescript-tools, typescript-utils

readme

Installation

npm package: @emmveqz/js-utils

npm install @emmveqz/js-utils

racePromisesIterator()

Returns an iterator, yielding the value of 'fulfilled' promises first.
In case a promise is rejected, the iterator will yield an Error instance instead.

Usage:

const promises: Promise<string>[] = [
  new Promise((resolve) => {
    setTimeout(() => {
      resolve('this will yield second')
    }, 3500)
  }),
  new Promise((resolve) => {
    setTimeout(() => {
      resolve('end')
    }, 7500)
  }),
  new Promise((_, reject) => {
    setTimeout(() => {
      reject('this will yield an error')
    }, 5500)
  }),
  new Promise((resolve) => {
    setTimeout(() => {
      resolve('this will yield first')
    }, 1500)
  }),
]

const someAsyncFunc = async () => {
  const itor = racePromisesIterator(promises)
  let result = await itor.next()

  while (!result.done) {
    console.log(result.value instanceof Error
      ? result.value.message
      : result.value)

    result = await itor.next()
  }
}