1.0.0 • Published 4 years ago
p-find-index v1.0.0
p-find-index
Wait for a left-most matched promise to be fulfilled
Install
$ npm i p-find-indexUsage
import {
find,
findIndex,
AggregateError,
MatchError
} from 'p-find-index'
import delay from 'delay'
const input = [
delay(100).then(() => 100),
delay(50).then(() => 10),
delay(60).then(() => 90)
]
const matcher = value => value > 50
console.log(await find(input, matcher)) // 100
console.log(await find(input, matcher, {leftMost: false})) // 90
console.log(await findIndex(input, matcher)) // 0
console.log(await findIndex(input, matcher, {leftMost: false})) // 2find(iterable, matcher, options): any
findIndex(iterable, matcher, options): number
Returns a cancelable Promise that is fulfilled when the first match is found. If options.leftMost is true, then it will find the left-most match of the iterable.
If you pass in cancelable promises each of which have an cancel() method, that method will be called when the target value has been found and the thenable is still pending.
- iterable
Iterable<Thenable | any>anIterablecollection of thenables/values to wait for. - matcher
Functionreceives the value resolved by the thenable and returns a boolean whether the value is matched some condition. - options
Object- leftMost
boolean = true
- leftMost
AggregateError
Exposed for instance checking.
MatchError
Exposed for instance checking.