Skip to main content
The Options interface defines configuration parameters for the meros() function.

Type definition

export interface Options {
  /**
   * Setting this to true will yield an array. In other words; instead of yielding once for every payload—we collect
   * all complete payloads for a chunk and then yield.
   *
   * @default false
   */
  multiple: boolean;
}

Properties

multiple
boolean
default:"false"
Controls how parts are yielded from the async generator.When false (default), the generator yields each part individually as soon as it’s parsed.When true, the generator collects all complete parts from each chunk and yields them as an array.This is an optimization for scenarios where you want to batch-process multiple parts synchronously, such as committing multiple GraphQL payloads to a store at once.

Usage

Default behavior (multiple: false)

import { meros } from 'meros';

// Default: yields individual parts
const parts = await fetch('/api').then(meros);

for await (const part of parts) {
  // Process one part at a time
  console.log(part.body);
}

Multiple mode (multiple: true)

import { meros } from 'meros';

const chunks = await fetch('/api').then((response) => 
  meros(response, { multiple: true })
);

// Yields arrays of parts
for await (const parts of chunks) {
  // parts is an array
  for (const part of parts) {
    console.log(part.body);
  }
}

GraphQL use case

The multiple option is particularly useful for GraphQL subscriptions with @defer and @stream directives:
import { meros } from 'meros';

const chunks = await fetch('/graphql', {
  method: 'POST',
  body: JSON.stringify({ query: '...' }),
}).then((response) => meros(response, { multiple: true }));

for await (const parts of chunks) {
  // Commit all parts to the store synchronously
  // instead of processing them one at a time
  store.commit(parts.map(p => p.body));
}

Type impact

The multiple option affects the return type:
// multiple: false or undefined
meros(response)
  // => Promise<Response | AsyncGenerator<Part<T, Fallback>>>

// multiple: true
meros(response, { multiple: true })
  // => Promise<Response | AsyncGenerator<ReadonlyArray<Part<T, Fallback>>>>

Performance considerations

  • Default mode (multiple: false): Lower latency - parts are processed immediately as they arrive
  • Multiple mode (multiple: true): Better throughput - reduces the number of async iterations and enables batch processing

Build docs developers (and LLMs) love