Oops, I made a mistake in the logic of `Scope` and coroutines.

According to the RFC, the following code behaves differently:

```php
currentScope()->spawn ... // This coroutine belongs to the Scope
spawn ... // This one is a child coroutine
```

I was sure that I had checked all the major edge cases. Sorry.
This will be fixed soon.

P.S. + 1 example:

<?php


declare(strict_types=1);

use Async\Scope;
use function Async\currentScope;

function fetchUrl(string $url): string {
    $ctx = stream_context_create(['http' => ['timeout' => 5]]);
    return file_get_contents($url, false, $ctx);
}

function fetchAllUrls(array $urls): array
{
    $futures = [];

    foreach ($urls as $url) {
        $futures[$url] = (spawn fetchUrl($url))->getFuture();
    }

    await currentScope();

    $results = [];

    foreach ($futures as $url => $future) {
        $results[$url] = $future->getResult();
    }

    return $results;
}

$urls = [
    'https://example.com',
    'https://php.net',
    'https://openai.com'
];

$results = await spawn fetchAllUrls($urls);
print_r($results);


---
Ed.

>

Reply via email to