JavaScript & jQuery Tutorials

deferred.rejectWith()

failCallbacks rejection.

Description

The deferred.rejectWith() Deferred method, rejects a Deferred object and calls any failCallbacks with the specified context and arguments.

Only the creator of a Deferred object should call this method. Other code can be stopped from changing Deferred object state or from reporting
Deferred object status by returning a restricted Promise object through the deferred.promise() method.

Callbacks are executed in the order they were added and each callback is passed the context from the deferred.rejectWith() method and args if any were specified.

Any failCallbacks added after the Deferred object enters the rejected state are executed immediately when they are added,
using the arguments that were passed to the deferred.rejectWith() method.

When using the deferred.rejectWith() method any arguments passed have to be wrapped in an array. You can call deferred.reject() in the desired context,
without the overhead of passing an array, as the method passes the context on to the callbacks fired.

Return

Reject a Deferred object and call any failCallbacks with the specified context and arguments.

In the example below when we press the left button the first time we create a Deferred object and use some deferred object methods on it. We then reject
the Deferred object with one argument and process any failCallbacks.

When we press the right button the first time we create a Deferred object and use some deferred object methods on it. We then reject
the Deferred object with two arguments and process any failCallbacks.