Add Generator-based coroutine()
function
#12
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This changeset adds a Generator-based
coroutine()
function for the upcoming v3 release.The API integrates nicely with existing Promise-based APIs and can be used to execute a Generator-based coroutine to "await" promises. Using Generator-based coroutines is an alternative to directly using the underlying promise APIs. For many use cases, this makes using promise-based APIs much simpler, as it resembles a synchronous code flow more closely.
A somewhat similar solution has been provided by @recoilphp for years already (big shout out!). After discussing this both internally and with Recoil (hey @jmalloc 👋), I think we all agree that having this as a first-class citizen in the ReactPHP ecosystem would be a fantastic opportunity!
I hear there are more news to follow soon with the imminent PHP 8.1 release, but in either case I still think this function provides a very nice integration point and also migration path towards the future of using promises! (More on that in a follow-up PR 🤫)
Builds on top of #11