INNER CODE UNIT · TypeScript

toAsyncIterable

enisdenjo/graphql-sse · src/client.ts:1046

function toAsyncIterable(
  val: ReadableStream | NodeJS.ReadableStream,
): AsyncIterable<string | Buffer | Uint8Array> {
  // node stream is already async iterable
  if (typeof Object(val)[Symbol.asyncIterator] === 'function') {
    val = val as NodeJS.ReadableStream;
    return val;
  }

  // convert web stream to async iterable
  return (async function* () {
    const reader = (val as ReadableStream).getReader();
    let result;
    do {
      result = await reader.read();
      if (result.value !== undefined) yield result.value;
    } while (!result.done);
  })();

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…