Skip to content Skip to sidebar Skip to footer

How Does A Thenable Object Get Rejected In JavaScript?

I know that a thenable has the then method, but how does Promise know the state of the thenable objected has moved to rejected? Example: Here, $.ajax is a thenable and can be duckt

Solution 1:

A Promises/A+ then method does take two callbacks - one for the fulfilment and one for the rejection case. You would not use the .then(…).catch(…) pattern but .then(…, …) - the second callback is the "catch case" (notice that .catch(…) is nothing but .then(null, …)).

This is how thenables are assimilated - when the second callback gets called, they reject the promise with the error. Example:

var rejectingPromise = Promise.resolve({
    then: function(onSuccess, onError) {
        onError(new Error);
    }
});

Post a Comment for "How Does A Thenable Object Get Rejected In JavaScript?"