Skip to content

Async Find Index

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

Async Find Index

Try iterable-async on RunKit

Docblock

/**
 * Async Find Index
 * Find an item's index 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 {Number} - an integer index, -1 if not found
 * @throws {TypeError}
 */
async function asyncFind(callback, [thisArg]) {...}

Examples

As a function

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

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

console.log('Resolved Index ' + found);
// Resolved Index 2

As a method

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

array.asyncFindIndex = asyncFindIndex;

const found = await array.asyncFindIndex(async element => {
    return element > 2;
});

console.log('Resolved Index ' + found);
// Resolved Index 2
Clone this wiki locally