p-truthy-race

2.0.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
})

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.0.2
    0
    • latest

Version History

Package Sidebar

Install

npm i p-truthy-race

Weekly Downloads

0

Version

2.0.2

License

AGPL-3.0-or-later

Unpacked Size

41.7 kB

Total Files

7

Last publish

Collaborators

  • jcam