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

Add new delay() function to delay program execution #69

Merged
merged 1 commit into from
Dec 6, 2022

Conversation

clue
Copy link
Member

@clue clue commented Dec 6, 2022

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's sleep() 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.

echo 'a';
React\Async\delay(1.0);
echo 'b';
// prints "a" at t=0.0s
// prints "b" at t=1.0s

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).

Copy link
Member

@SimonFrings SimonFrings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some nits

README.md Outdated Show resolved Hide resolved
src/functions.php Outdated Show resolved Hide resolved
Copy link
Member

@SimonFrings SimonFrings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

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

Successfully merging this pull request may close these issues.

3 participants