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 = ; const isValidPrice = { if ! return true; return false;}; const getCryptoPrice = { // logic to make your HTTP call};const apis = /* list of apis */ ; const promises = apis