@adviser/cement - v0.0.0
    Preparing search index...

    Function chunkyAsync

    • Processes an iterable in chunks asynchronously, committing each chunk when a condition is met.

      This function accumulates items from an iterable into chunks and commits them based on a split condition. Each commit is awaited before consuming the next item, making it safe for infinite iterables.

      • Infinite iterable support: Awaits each commit before consuming next item
      • Sequential processing: Commits execute one at a time in order
      • Error resilience: Uses exception2Result to wrap errors without aborting
      • Memory efficient: No unbounded queue buildup

      Type Parameters

      • T

        The type of items in the input iterable

      Parameters

      • options: ChunkyAsyncOptions<T>

        Configuration object containing input, splitCondition, commit, and onCommit

      Returns Promise<void>

      Promise that resolves when all chunks are processed

      await chunkyAsync({
      input: messages,
      splitCondition: (chunked) => chunked.length >= 100,
      commit: async (chunk) => {
      await api.batchSend(chunk)
      },
      onCommit: (result, idx) => {
      if (result.isErr()) {
      console.error(`Batch ${idx} failed:`, result.Err().message)
      }
      }
      })
      // Processing infinite generator
      function* infiniteStream() {
      let i = 0
      while (true) yield i++
      }

      // Safely processes chunks one at a time
      await chunkyAsync({
      input: infiniteStream(),
      splitCondition: (chunked) => chunked.length >= 1000,
      commit: async (chunk) => {
      await processChunk(chunk)
      }
      })