It doesn’t require a loop. This works:
function foo() {
yield 1;
yield 2;
yield 3;
}
One reason to use this is to use it as a similar feature as ‘await’ in Javascript.
It’s possible for example to build an async framework that uses yield for this purpose:
function foo() {
$response = yield asyncHttpRequest('GET', 'http://blabla');
}
This specific example is fictional, but it’s possible (and has been done).
I made such an async library. Docs here: https://sabre.io/event/coroutines/
There’s better maintained/more popular libraries out there though.
CLICK HERE to find out more related problems solutions.