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

Provide function that combines sleep() and timeout() #60

Closed
lcobucci opened this issue Jul 13, 2022 · 3 comments
Closed

Provide function that combines sleep() and timeout() #60

lcobucci opened this issue Jul 13, 2022 · 3 comments

Comments

@lcobucci
Copy link

lcobucci commented Jul 13, 2022

For an event-driven API with user interactions, I need to provide endpoints that delay the response for a while to wait for potential interactions and take an action once a timeout is reached.

I've implemented something in a possibly hacky way and believe it would be a potential feature for this lib:

namespace React\Promise\Timer;

use Closure;

use function React\Async\async;
use function React\Async\await;

/**
 * @param Closure():bool $stopCondition,
 * @param Closure():void $onTimeout
 */
function waitUntil(
    Closure $stopCondition,
    float $verificationInterval,
    Closure $onTimeout,
    float $timeout,
): void {
    try {
        $wait = async(static function () use ($stopCondition, $verificationInterval): void {
            while (! $stopCondition()) {
                await(sleep($verificationInterval)); // Timer\sleep() btw
            }
        });

        await(timeout($wait(), $timeout)); // Timer\timeout() btw
    } catch (TimeoutException) {
        $onTimeout();
    }
}

You certainly have ideas on how to turn this into a decent feature or just bin it.
I'm happy either way 😂

@SimonFrings
Copy link
Member

Hey @lcobucci, already a win if this helps you with your project 👍

Whether this will be a new feature or not depends on how many people need this for their use cases. If there's a demand for this I can see this as a future addition to the project. ^^

@lcobucci
Copy link
Author

lcobucci commented Aug 1, 2022

I completely understand it, my goal was mostly sharing and/or picking your brains. That solution works for me, which is enough atm 😂

@SimonFrings
Copy link
Member

Currently cleaning up some open tickets, doesn't seem there's currently much being discussed in here so I'll close this for now. We can still use this ticket for further discussion 👍

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

No branches or pull requests

2 participants