-
Notifications
You must be signed in to change notification settings - Fork 0
Async Find
zak-zaffar edited this page Dec 23, 2019
·
11 revisions
/**
* 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]) {...}
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
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