Hello.

> Why doesn’t scope implement Awaitable?

Let’s say there’s a programmer named *John* who is writing a library,
and he has a function that calls an external handler.
Programmer *Robert* wrote the `externalHandler`.

```php
function processData(string $url, callable $externalHandler): void
{
   ...
    $externalHandler($result);
}
```

John knows which contracts are inside processData, but he knows
nothing about `$externalHandler`.
>From the perspective of `processData`, `$externalHandler` acts as a
**black box**.
If John uses `await()` on that black box, it may lead to an infinite wait.

There are two solutions to this problem:

1. Delegate full responsibility for the program’s behavior to
*Robert*, meaning to `$externalHandler`
2. Establish a limiting contract

Therefore, if a `Scope` needs to be awaited, it can only be done
together with a cancellation token.

In real-world scenarios, awaiting a `Scope` during normal execution
makes no sense, because you have a `cancellation policy`.
This means that at any necessary moment you can dispose() the `Scope`
and thus interrupt the execution of tasks inside the black box.

For the `TaskGroup` pattern, which exists in the third version of the
RFC, awaiting is a relatively safe operation, because in this case we
assume that the code is written by someone who has direct access to
the agreements and bears full responsibility for any errors.

So, `Scope` is intended for components where responsibility is shared
between different programmers, while `TaskGroup` should be used when
working with a clearly defined set of coroutines.

Reply via email to