Skip to content

Async Filter

zak-zaffar edited this page Dec 23, 2019 · 18 revisions

Async For Each

Try iterable-async on RunKit

Docblock

/**
 * Async Filter
 * Filter an iterable object asynchronously and resolve when all callbacks are resolved
 * @async
 * @param {Function} callback - callback(currentValue, index, array)
 * @param {Object} [thisArg] - must be iterable
 * @return {Array}
 * @throws {TypeError}
 */
async function asyncFilter(callback, [thisArg]) {...}

Examples

As a function

const { asyncFilter } = require('iterable-async'),
  array = [1, 2, 3, 4, 5];

const found = await asyncFilter(async element => {
  return element > 2;
}, array);

console.log('Resolved Array ' , ...found);
// Resolved Array 3, 4, 5

As a method

const { asyncFind } = require('iterable-async'),
  array = [1, 2, 3, 4, 5];

array.asyncFilter = asyncFilter;

const found = await array.asyncFilter(async element => {
    return element > 2;
});
  console.log('Resolved Array ' + found);
// Resolved Array 3.4.5
Clone this wiki locally