conditional-race

1.0.0 • Public • Published

conditional-race

Build Status Maintainability Test Coverage

The conditionalRace function returns a Promise that is settled the same way (and takes the same value) as the first promise that settles and fulfills the passed condition function amongst the promises of the iterable passed as argument. If the iterable passed is empty, the promise returned will be forever pending.

If the iterable contains one or more non-promise value and/or an already resolved/rejected promise, then conditionalRace will resolve to the first of these values found in the iterable.

If no promise fulfills the conditional function, null is returned.

Install

$ npm install conditional-race

Usage

const conditionalRace = require('conditional-race');
 
const run = car => new Promise(resolve => setTimeout(() => resolve(car), car.speed));
 
const cars = {
  car1: {
    number: 1,
    color: 'blue',
    speed: 80,
  },
  car2: {
    number: 2,
    color: 'red',
    speed: 100,
  },
  car3: {
    number: 3,
    color: 'blue',
    speed: 90,
  },
};
 
// Should return the first blue car to win the race
const returnedCar = await conditionalRace([
  run(cars.car1),
  run(cars.car2),
  run(cars.car3),
], car => car.color === 'blue');
 
console.log(car.number) // 3

Readme

Keywords

none

Package Sidebar

Install

npm i conditional-race

Weekly Downloads

3

Version

1.0.0

License

BSD-3-Clause

Unpacked Size

7.5 kB

Total Files

11

Last publish

Collaborators

  • rodrigogs