Continuing the discussion from [[PHP-DEV] PHP True Async RFC - Stage 2](
https://discourse.thephp.foundation/t/php-dev-php-true-async-rfc-stage-2/1573/16
):
[quote="Crell, post:16, topic:1573"]
// Creates an async scope, in which you can create coroutines.
[/quote]
Yes, I understand what this is about.
Here’s a more specific example: launching two coroutines and waiting for
both.
```php
$scope = new Scope();
$scope->spawn(fn() => ...);
$scope->spawn(fn() => ...);
await $scope;
```
The downside of this code is that the programmer might forget to write
await $scope
.
Additionally, they constantly need to write $scope->
.
This code can be replaced with syntactic sugar:
```
async {
spawn ...
spawn ...
};
```
Am I understanding this correctly? Does it look nice? I think yes.
And at the same time, if the closing bracket is missing, the compiler will
throw an error, meaning you can't forget to await
.
That is the only advantage of this approach.
Now, let's talk about the downsides.
```
function task(): void {
spawn function() {
echo "What?";
};
async {
spawn ...
spawn ...
};
}
```
Let me explain.
You can write the spawn
operator **outside** the async
block. Why?
Because **nothing** can prevent you from doing so. It’s simply impossible.
After all, the function might already be executing inside another async
block outside its scope.
That’s exactly what happens an async
block is essentially always present
as soon as index.php
starts running.
It is necessary to determine whether this syntax truly provides enough
benefits compared to the direct implementation.
I will think about it.