Hi

> Assuming an awaitable is idempotent (such as the result of a coroutine), this 
> is an infinite loop. There’s a 99.9% chance that’s unintended, which is part 
> of the point of static analysis.

```php
foreach(($next = await($awaitable)) as $value) { }
```

executed as:
1. await($awaitable) - will return some value
2. foreach got this value
3. If the value has a valid interface, then foreach will iterate over it.

await($awaitable) -

It always returns only one value because under the hood it works like this:

```
function await(awaitable):
    # 1) Create a waker for the current coroutine
    waker = Waker(current_coroutine)

    # 2) Register an event handler inside the awaitable
    handler = function(result, error):
        waker.set_result(result, error)
        awaitable.remove_handler(handler) # detach
        Scheduler.enqueue(waker.coroutine)

    awaitable.add_handler(handler)

    try:
        # 3) Suspend the current coroutine until resumed
        Scheduler.suspend(current_coroutine)

        # 4) When the waker is triggered, return the stored result
        return waker.get_result()

    finally:
        # 5) Destroy/cleanup the waker object
        waker.dispose()                  # release buffers/slots
```

> The RFC says that for coroutines, but not Awaitable. It is undefined.
Sorry I can’t understand the meaning of this.

Thanks, Ed

Reply via email to