-
Notifications
You must be signed in to change notification settings - Fork 0
Async Filter
The asyncFilter() method creates a new array with all elements that pass the test implemented by the provided async function.
let newArray = await asyncArray.asyncFilter(
async function callback(currentValue[, index[, array]]) {
// test currentValue to pass filter
}
[, thisArg]
);-
callbackAsync function to execute on each element. It accepts between one and three arguments:-
currentValueThe current element being processed in the iterable object. -
index|optionalThe index ofcurrentValuein the iterable object. -
array|optionalThe iterable object thatasyncFilterwas called upon.
-
-
thisArgoptionalValue to use asthiswhen executingcallback.
A new array with the elements that pass the test. If no elements pass the test, an empty array will be returned.
asyncFilter() calls a provided callback function, which can be async, once for each element in an iterable object, and constructs a new array of all the values for which callback returns a value that coerces to true. callback is invoked only for indexes of the iterable object which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. Iterable object elements which do not pass the callback test are simply skipped, and are not included in the new array.
callback is invoked with three arguments:
- the value of the element
- the index of the element
- the iterable object being traversed
If a thisArg parameter is provided to asyncFilter, it will be used as the callback's this value. Otherwise, the value undefined will be used as its this value. The this value ultimately observable by callback is determined according to the usual rules for determining the this seen by a function.
asyncFilter does not mutate the iterable object on which it was called.
The range of elements processed by asyncFilter() is set before the first invocation of callback. Elements which are appended to the iterable object after the call to asyncFilter() begins will not be visited by callback. If existing elements of the iterable object are changed, or deleted, their value as passed to callback will be the value at the time asyncFilter() visits them; elements that are deleted are not visited.
TypeError
Type Error will be thrown if the callback is not a function or if the method is bound on an object which isn't iterable.
const { AsyncArray } = require('iterable-async'),
asyncArray = AsyncArray.from([1, 2, 3, 4, 5]);
const found = await asyncArray.asyncFilter(async element => {
return element > 2;
});
console.log('Resolved Array ', ...found);Resolved Array 3, 4, 5