@@ -113,7 +113,7 @@ function mergeFiles(string ...$files): string
113113 $taskGroup = new Async\TaskGroup(scope: $scope, captureResults: true);
114114
115115 foreach ($files as $file) {
116- $taskGroup->spawn( file_get_contents(...), $file);
116+ spawn with $taskGroup file_get_contents($file);
117117 }
118118
119119 return array_merge("\n", await $taskGroup);
@@ -210,6 +210,51 @@ function processJob(mixed $job): void {
210210}
211211```
212212
213+ #### Binding Coroutines to a PHP Object
214+
215+ ``` php
216+ class HttpClient
217+ {
218+ private Scope $scope;
219+
220+ public function __construct()
221+ {
222+ $this->scope = new Scope();
223+ }
224+
225+ public function request(array $data): \Async\Awaitable
226+ {
227+ return spawn with $this->scope use($data) {
228+ // This coroutine is bound to the MyClass instance
229+ };
230+ }
231+ }
232+
233+ $service = new HttpClient;
234+ $service->request(['login' => 'admin', 'password' => '1234']);
235+ // HttpClient instance will stop all coroutines bound to it.
236+ unset($service);
237+ ```
238+
239+ #### Tasks race
240+
241+ ``` php
242+ use Async\TaskGroup;
243+
244+ function fetchFirstSuccessful(string ...$apiHosts): string
245+ {
246+ $taskGroup = new Async\TaskGroup(captureResults: false);
247+
248+ foreach ($apiHosts as $host) {
249+ spawn with $taskGroup file_get_contents($host);
250+ }
251+
252+ // Get the first successful result
253+ return await $taskGroup->race(ignoreErrors: true);
254+ }
255+ ```
256+
257+
213258### Implementation requirements
214259
215260The implementation of this ** RFC** should be carried out in a way that minimizes changes to the ** PHP** core.
@@ -2323,7 +2368,7 @@ function fetchFirstSuccessful(string ...$apiHosts): string
23232368 $taskGroup = new Async\TaskGroup(captureResults: false);
23242369
23252370 foreach ($apiHosts as $host) {
2326- $taskGroup->spawn(function() use ($host) {
2371+ spawn with $taskGroup use ($host) {
23272372 $response = file_get_contents($host);
23282373
23292374 if($response === false) {
@@ -2951,9 +2996,9 @@ $scope->setExceptionHandler(function (Async\Scope $scope, Async\Coroutine $corou
29512996 echo "Caught exception: {$e->getMessage()}\n in coroutine: {$coroutine->getSpawnLocation()}\n";
29522997});
29532998
2954- $scope->spawn(function() {
2999+ spawn with $scope {
29553000 throw new Exception("Task 1");
2956- }) ;
3001+ };
29573002
29583003await $scope;
29593004```
@@ -3058,9 +3103,9 @@ A **responsibility point** is code that explicitly waits for the completion of a
30583103``` php
30593104$scope = new Scope();
30603105
3061- $scope->spawn(function() {
3106+ spawn with $scope {
30623107 throw new Exception("Task 1");
3063- }) ;
3108+ };
30643109
30653110try {
30663111 await $scope;
0 commit comments