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 timeoutWithDefault function #245

Merged
merged 4 commits into from
Nov 25, 2018
Merged

Conversation

prolic
Copy link
Contributor

@prolic prolic commented Nov 2, 2018

see #244


$promise->onResolve(function ($exception, $value) use ($deferred, $default) {
if ($exception) {
$deferred->resolve($default);
Copy link
Member

Choose a reason for hiding this comment

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

An exception should probably be thrown here instead of resulting in the default value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, give me a minute.

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't make use of the timeout function, but rather adapt the code of it to suit this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No wait, that's the whole purpose of the default value. You timeout with a default and when you'll receive a timeout exception, you'll get the default value.

Copy link
Member

Choose a reason for hiding this comment

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

You'll get the default on a timeout, but not if the promise fails?

@prolic
Copy link
Contributor Author

prolic commented Nov 3, 2018

@kelunik updated

@trowski
Copy link
Member

trowski commented Nov 3, 2018

This is way less code:

function timeoutWithDefault($promise, int $timeout, $default = null): Promise
{
    return call(function () use ($promise, $timeout, $default) {
        try {
            return yield timeout($promise, $timeout);
       } catch (TimeoutException $excetpion) {
            return $default;
       }
    });
}

This is so little code that I question the need for this, but also so little that I don't really care maintance-wise either.

@prolic
Copy link
Contributor Author

prolic commented Nov 3, 2018

@trowski: @kelunik asked me not to reuse the timeout function. I know it's not much code, but having those 7 lines of code 50 times in your code base really sucks :)

@trowski
Copy link
Member

trowski commented Nov 3, 2018

I think mostly because you originally didn't use timeout() within a coroutine, which then makes it nearly as complicated. Otherwise I see no reason to not reuse the function.

You can define functions in your code base you know. 😉 But yes, I can see where this can be useful for many people, so let's add it.

});
}

public function testNonPromise()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@trowski I tested your suggested implementation and then this test fails, because the TypeError is not thrown.

Copy link
Member

Choose a reason for hiding this comment

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

It should be…

Copy link
Member

@trowski trowski Nov 3, 2018

Choose a reason for hiding this comment

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

Oh, right, because it's not resolving the promise with the TypeError. Fine, then it needs to be this way:

function timeoutWithDefault($promise, int $timeout, $default = null): Promise
{
    $promise = timeout($promise, $timeout);

    return call(function () use ($promise, $default) {
        try {
            return yield $promise;
        } catch (TimeoutException $exception) {
            return $default;
        }
    });
}

return call(function () use ($promise, $default) {
try {
return yield $promise;
} catch (TimeoutException $excetpion) {
Copy link
Member

Choose a reason for hiding this comment

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

Totally my fault, but typo: $excetpion 😄

@trowski
Copy link
Member

trowski commented Nov 3, 2018

The style fixer is going to yell at you. 🙄
Otherwise looks good.

@prolic
Copy link
Contributor Author

prolic commented Nov 3, 2018

done

@kelunik kelunik merged commit 4a98cc4 into amphp:master Nov 25, 2018
@prolic prolic deleted the timeoutWithDefault branch April 25, 2020 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants