Identifies the element type of the iterator
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.
OptionalthisArg: 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<T> and not an
Iterable<T>.
If you are targetting a mixed environment you SHOULD either
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
callbackFnfunction 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 thecallbackFnfunction.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
iteris both an Iterable<T> and Iterator<T> the Iterator<T> interface takes precedence. And if an iterable and does not have aSymbol.iteratorproperty then theiterwill be used as the iterator.The
callbackFnmay executesynchronouslyorasynchronouslyand if thecallbackFnreturns aPromisethen the next iteration will not be called until the promise is resolved. If thecallbackFnreturns aPromisethat is rejected then the iteration will stop and the promise returned by iterForEachAsync will be rejected with the same error.