Function iterForOfAsync

  • 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 the callbackFn 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 iter is both an Iterable and Iterator the Iterator interface takes precedence. And if an iterable and does not have a Symbol.iterator property then the iter will be used as the iterator.

    The callbackFn may execute synchronously or asynchronously and if the callbackFn returns a Promise then the next iteration will not be called until the promise is resolved. If the callbackFn returns a Promise that is rejected then the iteration will stop and the promise returned by iterForEachAsync will be rejected with the same error.

    Type Parameters

    • T = any

      Identifies the element type of the iterator

    Parameters

    • iter:
          | Iterator<T, any, undefined>
          | Iterable<T>
          | AsyncIterator<T, any, undefined>
          | AsyncIterable<T>

      The iterator or iterable of elements to be searched.

    • callbackFn: ((value: T, count: number, iter?: Iterator<T, any, undefined> | AsyncIterator<T, any, undefined>) => number | void | IPromise<number | void>)

      A asynchronous or synchronous function that accepts up to three arguments. iterForEach calls the callbackfn function one time for each element in the iterator.

        • (value, count, iter?): number | void | IPromise<number | void>
        • Parameters

          • value: T
          • count: number
          • Optionaliter: Iterator<T, any, undefined> | AsyncIterator<T, any, undefined>

          Returns number | void | IPromise<number | void>

    • OptionalthisArg: any

      An 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.

    Returns void | number | IPromise<void | number>

    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 and not an Iterable. If you are targetting a mixed environment you SHOULD either

    • only use the polyfill Symbol's provided by this library
    • ensure that you add any symbol polyfills BEFORE these utilities iterForOfAsync

    0.5.0

    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