Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider leveraging return instead of yield for coroutines #170

Open
mikethea1 opened this issue Apr 16, 2024 · 0 comments
Open

Consider leveraging return instead of yield for coroutines #170

mikethea1 opened this issue Apr 16, 2024 · 0 comments

Comments

@mikethea1
Copy link

Description
I'm used to working in languages with async/await and so I was excited to discover Coroutine::of. However, after using it for a bit I find the behavior where yield plays a dual role of await and also return to be confusing and result in less-readable code.

I'm wondering if you considered leveraging return rather than yield to return from the async function, restricting yield to only the await role.

Example
How it works today (as I understand it):

if (...) {
    yield syncExpression(); // this is just a return value; only obvious if you see the upcoming return
    return;
}

if (...) {
    yield asyncExpression(); // this is an await and also a return value; only obvious if you see the upcoming return
    return;
}

$temp = yield asyncExpression(); // this is just an await, unless it happens to be the last yield

yield $temp + 2; // this is a return value, even though there is no following return (because the function ends)

How I maybe expected it to work:

if (...) {
    return syncExpression(); // this is just a return value
}

if (...) {
    return yield asyncExpression(); // await a value, then return the result
}

$temp = yield asyncExpression(); // this is just an await

return $temp + 2; // this is just a return value
// if we get to the end of the function without returning, we have a void promise

Additional context
I'm a PHP novice; so if there are good reasons why this was done, I'd love to understand the details!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant