---
title: "write(_:timeout:completionHandler:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlsessionstreamtask/write(_:timeout:completionhandler:)"
---

# write(_:timeout:completionHandler:)

Asynchronously writes the specified data to the stream, and calls a handler upon completion.

## Declaration

```swift
func write(_ data: Data, timeout: TimeInterval, completionHandler: @escaping @Sendable ((any Error)?) -> Void)
```

```swift
func write(_ data: Data, timeout: TimeInterval) async throws
```

## Parameters

- `data`: The data to be written.
- `timeout`: A timeout for writing bytes. If the write is not completed within the specified interval, the write is canceled and the completionHandler is called with an error. Pass 0 to prevent a write from timing out.
- `completionHandler`: The completion handler to call when all bytes are written, or an error occurs. This handler is executed on the delegate queue. This completion handler takes the following parameter:

## Discussion

Discussion There is no guarantee that the remote side of the stream has received all of the written bytes at the time that completionHandler is called, only that all of the data has been written to the kernel.

## See Also

### Reading and writing data

- [readData(ofMinLength:maxLength:timeout:completionHandler:)](foundation/urlsessionstreamtask/readdata(ofminlength:maxlength:timeout:completionhandler:).md)
