Skip to content

Conversation

@jsor
Copy link
Member

@jsor jsor commented Nov 23, 2016

resolve supports foreign thenables (introduced in #52), so should reject.

@jsor jsor added this to the v2.5 milestone Nov 23, 2016
@jsor jsor changed the base branch from master to 2.x November 23, 2016 20:35
@jsor
Copy link
Member Author

jsor commented Nov 25, 2016

Request for review @clue @WyriHaximus @cboden

Copy link
Member

@WyriHaximus WyriHaximus left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

Copy link
Member

@clue clue left a comment

Choose a reason for hiding this comment

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

Note sure how useful this actually is, but I don't really mind despite 👍

The instanceof check should be significantly faster and I suppose this may be a function that is called quite often. Does it make sense to OR both conditions and rely on short-circuiting here?

@jsor
Copy link
Member Author

jsor commented Nov 25, 2016

Note sure how useful this actually is, but I don't really mind despite 👍

It is more about being consistent.

The instanceof check should be significantly faster and I suppose this may be a function that is called quite often. Does it make sense to OR both conditions and rely on short-circuiting here?

Usually, you will do reject(new \Exception) or just reject(). So, the method_exists will be called anyway in most cases.

The optimization would be to turn the logic around

function reject($promiseOrValue = null)
{
    if (
        !$promiseOrValue ||
        $promiseOrValue instanceof \Throwable ||
        $promiseOrValue instanceof \Exception ||
        !is_object($promiseOrValue) ||
        !method_exists($promiseOrValue, 'then')
    ) {
        return new RejectedPromise($promiseOrValue);
    }

    return resolve($promiseOrValue)->then(function ($value) {
        return new RejectedPromise($value);
    });
}

@jsor
Copy link
Member Author

jsor commented Nov 28, 2016

Closing as i changed my mind. We probably enforce exceptions as rejection reasons in the future (#46). So, no need to support/encourage usage of thenables as rejection reasons.

@jsor jsor closed this Nov 28, 2016
@jsor jsor deleted the 2.x-reject-thenables branch November 28, 2016 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants