Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Deferred

A Deferred object is returned by the obsolete Promise.defer() method to provide a new promise along with methods to change its state.

Gecko 30 note

Starting from Gecko 30, this object is obsolete and should not be used anymore. Use the new Promise() constructor instead (or use the above backwards/forwards compatible Deferred function given below). For example the equivalent of

var deferred = Promise.defer();
doSomething(function cb(good) {
    if (good)
        deferred.resolve();
    else
        deferred.reject();
});
return deferred.promise;

would be

return new Promise(function(resolve, reject) {
    doSomething(function cb(good) {
        if (good)
            resolve();
        else
            reject();
    });
});

 

 

Method overview

void resolve([optional] aValue);
void reject([optional] aReason);

Properties

Attribute Type Description
promise Read only Promise A newly created promise, initially in the pending state.

Methods

resolve()

Fulfills the associated promise with the specified value, or propagates the state of an existing promise. If the associated promise has already been resolved, either to a value, a rejection, or another promise, this method does nothing.

Note: This function is bound to its associated promise when Promise.defer() is called, and can be called with any value of this.
Note: Calling this method with a pending promise as the aValue argument, and then calling it again with another value before the promise is resolved or rejected, will have no effect the second time, as the associated promise is already resolved to the pending promise value as its resolution.
void resolve(
  aValue
);
Parameters
aValue Optional
If this value is not a promise, including undefined, it becomes the fulfillment value of the associated promise. If this value is a promise, then the associated promise will be resolved to the passed promise, and follow the state as the provided promise (including any future transitions).

reject()

Rejects the associated promise with the specified reason. If the promise has already been resolved, either to a value, a rejection, or another promise, this method does nothing.

Note: This function is bound to its associated promise when Promise.defer() is called, and can be called with any value of this.
void reject(
  aReason
);
Parameters
aReason Optional

The rejection reason for the associated promise. Although the reason can be undefined, it is generally an Error object, like in exception handling.

Note: This argument should not be a promise. Specifying a rejected promise would make the rejection reason equal to the rejected promise itself, and not its rejection reason.

Backwards and forwards compatible helper

This Deferred function can be used for backwards and forwards compataibilty, due to a change that took place in Firefox 30.

function Deferred() {
	// update 062115 for typeof
	if (typeof(Promise) != 'undefined' && Promise.defer) {
		//need import of Promise.jsm for example: Cu.import('resource:/gree/modules/Promise.jsm');
		return Promise.defer();
	} else if (typeof(PromiseUtils) != 'undefined'  && PromiseUtils.defer) {
		//need import of PromiseUtils.jsm for example: Cu.import('resource:/gree/modules/PromiseUtils.jsm');
		return PromiseUtils.defer();
	} else {
		/* A method to resolve the associated Promise with the value passed.
		 * If the promise is already settled it does nothing.
		 *
		 * @param {anything} value : This value is used to resolve the promise
		 * If the value is a Promise then the associated promise assumes the state
		 * of Promise passed as value.
		 */
		this.resolve = null;

		/* A method to reject the assocaited Promise with the value passed.
		 * If the promise is already settled it does nothing.
		 *
		 * @param {anything} reason: The reason for the rejection of the Promise.
		 * Generally its an Error object. If however a Promise is passed, then the Promise
		 * itself will be the reason for rejection no matter the state of the Promise.
		 */
		this.reject = null;

		/* A newly created Pomise object.
		 * Initially in pending state.
		 */
		this.promise = new Promise(function(resolve, reject) {
			this.resolve = resolve;
			this.reject = reject;
		}.bind(this));
		Object.freeze(this);
	}
}

and use this function as would Promise.defer:

function funcWithDefer() {
  var deferred = new Deferred();
  var promise = deferred.promise;
  promise.then(
    function(aVal) {
      console.log('Fullfilled - ', aVal);
    },
    function(aReason) {
      console.error('Rejected - aReason:', aReason)
    }
  );
 
  //deferred.resolve('aVal of resolved');
  deferred.reject('reject val');
 
  return promise;
}

var promise_funcWithDefer = funcWithDefer();
promise_funcWithDefer.then(
  function(aVal) {
    console.log('Fullfilled - promise_funcWithDefer - ', aVal);
  },
  function(aReason) {
    var refObj = {name:'promise_funcWithDefer', aReason:aReason};
    console.error('Rejected - promise_funcWithDefer - ', refObj);
  }
).catch(
  function(aCatch) {
    console.error('Caught - promise_funcWithDefer - ', aCatch);
    throw aCatch;
  }
);

See also

Document Tags and Contributors

Tags: 
 Contributors to this page: Noitidart, P.A., DomenicDenicola
 Last updated by: Noitidart,