Parameters
onFulfilledOptionalA
Functioncalled if thePromiseis fulfilled. This function has one argument, thefulfillment value. If it is not a function, it is internally replaced with an "Identity" function (it returns the received argument).onRejectedOptionalA
Functioncalled if thePromiseis rejected. This function has one argument, therejection reason. If it is not a function, it is internally replaced with a "Thrower" function (it throws an error it received as argument).
Return value
Once a Promise is fulfilled or rejected, the respective handler function (onFulfilled or onRejected) will be called asynchronously (scheduled in the current thread loop). The behavior of the handler function follows a specific set of rules. If a handler function:
- returns a value, the promise returned by
thengets resolved with the returned value as its value. - doesn't return anything, the promise returned by
thengets resolved with anundefinedvalue. - throws an error, the promise returned by
thengets rejected with the thrown error as its value. - returns an already fulfilled promise, the promise returned by
thengets fulfilled with that promise's value as its value. - returns an already rejected promise, the promise returned by
thengets rejected with that promise's value as its value. - returns another pending promise object, the resolution/rejection of the promise returned by
thenwill be subsequent to the resolution/rejection of the promise returned by the handler. Also, the resolved value of the promise returned bythenwill be the same as the resolved value of the promise returned by the handler.
When a value is returned from within a
then handler, it will effectively return Promise.resolve(<value returned by whichever handler was called>).
评论
发表评论