Namespace: Basic:Deferred

Basic:Deferred

Source:

Members

(static) Deferred

Class that wraps a Promise, to allow resolving and rejecting outside the Promise's function scope. This allows for decoupled handling of states and handling promises as references in a distributed context, like a class, where a Deferred might then represent an async state.

Deferreds also provide accessible status information, normal Promises do not have. Accessing the "status" property returns the current status, being either "pending", "fulfilled" or "rejected". You may also check if the Deferred has been settled via "isSettled()". If you want to provide a preliminary result, available before the promise has settled, you may set this result as a payload using the "provision" property.

This follows ideas by jQuery and Q Promises:

  • https://api.jquery.com/jQuery.Deferred/
  • https://github.com/kriskowal/q/wiki/Coming-from-jQuery#deferreds-promises-resolvers

Keep in mind, that Promises might need a polyfill such as core-js.

For details, see class documentation below.

Source:
See:
Example
const doStuff = new Deferred();
doStuff.provision = 'provisional value';
doStuff
  .then(value => { alert(`yeah, ready with "${value}"!`); })
  .catch(error => { console.error(error); })
  .finally(() => { console.info('has been settled'); })
;
if( foobar === 42 ){
  doStuff.resolve(42);
} else {
  doStuff.reject(new Error('not 42!'));
}
console.info(doStuff.status);