JavaScript adapter

datastream.to_async_iterable exposes a synchronous stream to JavaScript’s for await loop. Both this function and the AsyncIterable(a) type are available only when compiling for JavaScript.

pub type AsyncIterable(a)

pub fn to_async_iterable(stream: Stream(a)) -> AsyncIterable(a)

With callbacks that return normally, each call to the iterator’s next() method pulls one element synchronously and returns a promise for the result. The adapter does not turn asynchronous input, such as a fetch response body, into a Gleam stream. Handle that input in JavaScript and pass completed values or batches into Gleam.

Example

In a Gleam project named app, put this function in src/app.gleam:

import datastream

pub fn lines_for_host() -> datastream.AsyncIterable(String) {
  datastream.from_list(["A\nB", "\nC\n"])
  |> datastream.lines
  |> datastream.to_async_iterable
}

Build with gleam build --target javascript, then consume the exported function from JavaScript:

import { lines_for_host } from "./build/dev/javascript/app/app.mjs";

for await (const line of lines_for_host()) {
  console.log(line);
  if (line === "B") break;
}

This prints A and B. Breaking out of the loop calls the iterator’s return() method, which closes the remaining stream. Calling return() before the first pull leaves a lazy resource unopened. Calling it again does nothing.

An adapter is a single-use iterator: calling [Symbol.asyncIterator]() returns the same iterator. Call to_async_iterable again to start another run of a replayable stream. Shared broadcast, broadcast_bounded, and unzip consumers remain single-use even when wrapped in a new adapter.

If a source callback calls return(), the outer next() finishes with done: true. Cleanup uses the latest continuation after the callback returns, or the resource’s normal cleanup if it returns Done. Calling next() from inside a pull returns a promise rejected with TypeError; it does not repeat the upstream pull or stop the outer call.

The adapter also implements throw(error): it closes the stream and returns a promise rejected with error. If a callback throws during a pull, every resource the iterator opened closes once at its latest state, and next() returns a promise rejected with the callback’s exception. An exception from close rejects the promise returned by return(); no iterator method throws synchronously, and an exception from close does not replace the error passed to throw(error) or thrown by a callback.

✨ Search Document