Skip to content

Async Find

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

Async Find

Try iterable-async on RunKit

Docblock

/**
 * Async Find
 * Find an item in an iterable object asynchronously and resolve when found or all callbacks resolve
 * @async
 * @param {Function} callback - callback(currentValue, index, array)
 * @param {Object} [thisArg] - must be iterable
 * @return {any}
 * @throws {TypeError}
 */
async function asyncFind(callback, [thisArg]) {...}

Examples

As a function

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

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

console.log('Resolved element' + found);
// Resolved element 3

As a method

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

array.asyncFind = asyncFind;

await array.asyncFind(async element => {
  console.log('Resolved element ' + element);
});
// Resolved element 3
Clone this wiki locally