# All files / src/_utils runInQueue.ts

92.3% Statements 12/13
33.33% Branches 1/3
100% Functions 5/5
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    7x

    7x
    42x
    56x
    56x
    56x
    28x

    7x
    7x

     interface Callback<T> {
      (item: T): Promise<any>
    }

    function workers<T>(
      threads: number,
      iterator: IterableIterator<T>
    ): IterableIterator<T>[] {
      return new Array(threads).fill(iterator)
    }

    function run<T>(func: Callback<T>) {
      return async (iterator: IterableIterator<T>) => {
        while (Itrue) {
          const result = iterator.next()
          if (result.done) break
          await func(result.value)
        }
      }
    }

    export function runInQueue<T>(array: T[], func: Callback<T>, Iconcurrency = 1) {
      return workers(concurrency, array.values()).map(run(func))
    }