Skip to content

mightyplow/Promise.defer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Promise.defer

emulates the defer() method of the q library

Sometimes you need a value which gets resolved later. The $q library therefore provides the defer() method. Sadly it is not implemented in the native ES6 Promise object. This small snippet adds the defer method to the Promise object, which is available in chrome by default (but it's deprecated).

API

After the snippet got executed, there is a defer() method available on the global Promise object (if it wasn't there before).

This method returns an object with the following properties and methods:

properties

promise
    the actual promise, which gets resolved or rejected

methods

resolve([resason]) 
    resolves the deferred value
    
reject([reason]) 
    rejects the deferred value

usage

Create a deferred value by calling

var deferred = Promise.defer();

Now you can use the promise property.

deferred.promise.then(function (data) {
    console.log(data);
});

Finally, when the deferred value gets resolved, the then() method gets executed.

deferred.resolve('foobar')';

example

See the example.js file to see a full usage example. You can easily execute it with node.js to run it. The defer() method of the V8 engine gets deleted before the snippet inclusion to ensure using the new one (only for example).

About

emulates the q.defer() method

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published