Add new delay()
function to delay program execution
#69
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 new
delay()
function to delay program execution. This function will only return after the given number of$seconds
have elapsed. Unlike PHP'ssleep()
function, this function may not necessarily halt execution of the entire process thread. Instead, it allows the event loop to run any other events attached to the same loop until the delay returns.This function is especially useful if you want to delay the program execution of a particular routine, such as when building a simple polling or retry mechanism. Internally, this function is implemented as the equivalent of
React\Async\await(React\Promise\Timer\sleep($seconds))
but does not require any additional dependencies (reactphp/promise-timer#51) and is arguably much easier to use.If this PR is merged, I will backport the changes to v3 and v2. The function should work exactly the same across all versions for simple use cases, but only v4 takes advantage of fibers (#15).