Publish your own packages to the world's most popular software ecosystem with npm Pro.Get started »

p-truthy-race

1.1.2 • Public • Published

p-truthy-race

This is a small utility function that does some nifty promise logic.

The goal is to race a collection of promises against one another and return the first to resolve true against a evaluator function.

For example: say you wish to send a bunch of API calls to crypto-exchanges to get the price of BitCoin. You could initiate them all, then use p-truthy-race to determine which has returned the fastest. Then display this to the user.

Documentation

pTruthyRace

  • evaluator (function, required) - the function to evaluate the results against.
    • accepts a single param
    • returns a boolean
  • promises (Promise[], required) - array of promises
  • alternative (any, optional) - If all promises resolve but none matches the evaluator function, return this value as the final result. Defaults to false.
  • returns - the first result that resolves true against the evaluator, otherwise the alternative value

Example Implementation

const { pTruthyRace } = require('p-truthy-race');
 
const isValidPrice = (result) => {
    if (!isNaN(result)) {
        return true;
    }
    return false;
};
 
const getCryptoPrice = (api) => {
    // logic to make your HTTP call
};
const apis = [ /* list of apis */ ];
 
 
const promises = apis.map(api => getCryptoPrice(api))
 
pTruthyRace(isValidPrice, promises).then(result => {
    // do what you must with the first result to resolve true
}).catch(error => {
    // do what you must with any promises that reject
})

Install

npm i p-truthy-race

DownloadsWeekly Downloads

5

Version

1.1.2

License

AGPL-3.0-or-later

Unpacked Size

311 kB

Total Files

8

Last publish

Collaborators

  • avatar