Skip to content

Commit 3263a57

Browse files
committed
Document asynchronous invocation [skip ci]
1 parent 3be9cea commit 3263a57

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,35 @@ Body:
138138
* `$response->stream(io.streams.InputStream $in[, int $size])` will stream a response
139139
* `$response->body(string $bytes)` will write the given raw bytes to the response
140140

141+
Asynchronous invocation
142+
-----------------------
143+
144+
The following code will run the upload function asynchronously, continuing to serve requests while file contents are being transmitted.
145+
146+
```php
147+
use io\Folder;
148+
use web\rest\{Async, Post, Resource, Response};
149+
150+
#[Resource('/api')]
151+
class Uploads {
152+
public function __construct(private Folder $folder) { }
153+
154+
#[Post('/files')]
155+
public function upload(#[Request] $req) {
156+
return new Async(function() use($req) {
157+
if ($multipart= $req->multipart()) {
158+
159+
foreach ($multipart->files() as $file) {
160+
yield from $file->transmit($this->folder);
161+
}
162+
}
163+
164+
return Response::ok();
165+
});
166+
}
167+
}
168+
```
169+
141170
See also
142171
--------
143172

0 commit comments

Comments
 (0)