# WritableStream

The WritableStream interface is a part of the Streams API that provides a standardized way to write streaming data to a sink (destination). It comes with its own queuing and backpressure mechanism to ensure that the data is written in an efficient way.

In an Azion function, a `WritableStream` lets you produce output incrementally instead of buffering an entire response in memory. This is especially useful when piping or transforming large payloads, because the built-in backpressure keeps the function from overwhelming the destination while data streams through close to the end user.

## Constructor

[WritableStream()](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/WritableStream)
Creates a new writable object.

## Instance properties

[WritableStream.locked](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/locked)
A boolean indicating whether the WritableStream is locked to a writer.

## Instance methods

[WritableStream.abort()](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/abort)
Aborts the stream, signaling that the producer can no longer successfully write to the stream and it's to be immediately moved to an error state, with any queued writes discarded.

[WritableStream.close()](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/close)
Closes the stream.

[WritableStream.getWriter()](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/getWriter)
Returns a new instance of WritableStreamDefaultWriter and locks the stream to that instance. While the stream is locked, no other writer can be acquired until this one is released.

## Use cases

- Streaming a large or generated response body to the client chunk by chunk instead of holding it all in memory.
- Acting as the writable end of a `TransformStream` when you need to rewrite or filter a payload as it passes through a function.
- Piping data from an upstream `ReadableStream` into a sink while the built-in backpressure throttles the producer to match the consumer.
- Aborting an in-progress write with `abort()` when an error occurs, so partial or invalid output is discarded cleanly.

## Related resources

- [WritableStream on MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream)
- [Runtime APIs](/en/documentation/products/applications/functions/runtime-apis/javascript/fetch/)

For more information on [WritableStream](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream) visit MDN Web Docs.