The iterator or iterable of elements to be searched.
A asynchronous
or synchronous
function that accepts up to three arguments. iterForEach
calls the callbackfn function one time for each element in the iterator.
Optional
thisArg: anyAn object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, null or undefined the iterator will be used as the this value.
If Symbols are NOT supported then the iterable MUST be using the same polyFill for the well know symbols, as used
by the library. If the iterable is using a different polyFill then the iter
MUST be an Iterator
const items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10'];
const copyItems = [];
// using async / await
let result = await iterForOfAsync(items, async (value, index) => {
copyItems.push(value);
if (index === 5) {
return -1; // Stop the iteration
}
await createTimeoutPromise(100); // Wait 100ms before processing the next item, you could also just return the promise
})
console.log(result); // returns -1 if the loop was stopped, otherwise returns undefined
// using doAwait
doAwait(iterForOfAsync(items, (value, index) => {
copyItems.push(value);
if (index === 5) {
return -1; // Stop the iteration
}
return createTimeoutPromise(100); // Wait 100ms before processing the next item, you could also just return the promise
}), (result) => {
console.log(result); // returns -1 if the loop was stopped, otherwise returns undefined
Calls the provided
callbackFn
function once for each element in the iterator or iterator returned by the iterable and processed in the same order as returned by the iterator. As with the arrForEachAsync you CAN stop or break the iteration by returning -1 from thecallbackFn
function.The order of processing is not reset if you add or remove elemenets to the iterator, the actual behavior will depend on the iterator implementation.
if the passed and Iterator the Iterator interface takes precedence. And if
an iterable and does not have a
iter
is both an IterableSymbol.iterator
property then theiter
will be used as the iterator.The
callbackFn
may executesynchronously
orasynchronously
and if thecallbackFn
returns aPromise
then the next iteration will not be called until the promise is resolved. If thecallbackFn
returns aPromise
that is rejected then the iteration will stop and the promise returned by iterForEachAsync will be rejected with the same error.