Skip to content

Consider leveraging return instead of yield for coroutines #170

@mikethea1

Description

@mikethea1

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions