WritableStream and sequential writes
Quick answer
A WritableStream accepts chunks through a sink. A writer exposes promises that report completion or failure of write and close operations.
How it works
new WritableStream({write(chunk),close(),abort(reason)}) defines a sink. getWriter() locks the stream. Await writer.write(chunk) when producing sequentially; writer.close() requests successful completion after queued writes. writer.abort(reason) requests unsuccessful termination. releaseLock() releases ownership; it does not finish the sink.
Local examples
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const s=new WritableStream();const w=s.getWriter();console.log(s.locked);w.releaseLock();console.log(s.locked);})()Expected console output:
truefalseLock ownership is explicit.
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const out=[];const s=new WritableStream({write(v){out.push(v);},close(){out.push("end");}});const w=s.getWriter();await w.write("a");await w.write("b");await w.close();w.releaseLock();console.log(JSON.stringify(out));})()Expected console output:
["a","b","end"]The sink completes both writes before close.
Common mistakes
Do not enqueue unlimited writes while ignoring promises. A rejected write needs error handling.
Practice
Create a sink storing strings in an array; write two chunks and verify that close executes last.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
WritableStream and sequential writes — Knowledge Check
Apply the concept and reason about local example output.
Which hook receives each chunk in a writable sink?
Does releasing a writer lock automatically close the stream?
Why await a write promise?
What is the expected console output of WritableStream and sequential writes example 1?
(async () => {const s=new WritableStream();const w=s.getWriter();console.log(s.locked);w.releaseLock();console.log(s.locked);})()What is the expected console output of WritableStream and sequential writes example 2?
(async () => {const out=[];const s=new WritableStream({write(v){out.push(v);},close(){out.push("end");}});const w=s.getWriter();await w.write("a");await w.write("b");await w.close();w.releaseLock();console.log(JSON.stringify(out));})()