Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

WritableStream — writable stream

WritableStream serializes writes of data chunks to a sink. Promises let producers control their pace.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

new WritableStream({ start(controller) {}, write(chunk, controller) {}, close() {}, abort(reason) {} }, strategy)

WritableStream is a Web API defined by the Streams Standard, not ECMAScript. Check availability in the target runtime; Node 24 does not establish browser support.

Optional sink callbacks: start(controller) initializes; write(chunk, controller) writes sequentially after initialization; close() finishes after writes; abort(reason) cleans up on abortion. Abortion discards queued writes instead of completing them.

getWriter() locks the stream. Then use writer.write(), writer.close() and writer.abort(); stream.close() and stream.abort() reject while locked. releaseLock() unlocks without closing.

Awaiting write() paces production; writer.ready signals relieved backpressure. Queuing alone does not bound memory. Successful writing does not guarantee durable storage.

Examples

async function main() {
  const events = [];
  const stream = new WritableStream({
    start() { events.push('start'); },
    write(chunk) { events.push(`write:${chunk}`); },
    close() { events.push('close'); },
    abort(reason) { events.push(`abort:${reason}`); }
  });
  const writer = stream.getWriter();
  try {
    await writer.write('A');
    await writer.write('B');
    await writer.close();
  } finally {
    writer.releaseLock();
  }
  console.log(events.join(' | '));
}
main().catch(console.error);

Result

start | write:A | write:B | close

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources