Skip to content

Instantly share code, notes, and snippets.

@thebigredgeek
Last active December 30, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebigredgeek/7904345 to your computer and use it in GitHub Desktop.
Save thebigredgeek/7904345 to your computer and use it in GitHub Desktop.
Simple Promise API
(function(scope){ //injected scope to bind
scope.QLite = {};
var publicMembers = scope.QLite, //bind publicMembers to the injected scope
privateMembers = {}; //private members hash for internal closure members
privateMembers.promise = function(){ //the promise object
var self = this,
queue = {};
queue.failure = []; //contains failure callbacks
queue.success = []; //contains success callbacks
queue.final = []; //contains final callbacks
self.resolve = function(data){ //triggered upon successful resolution, this usually isn't directly on the promise object
var i;
while(queue.success.length > 0){
queue.success.pop()(data); //pop the success cseallbacks and execute, passing data
}
while(queue.final.length > 0){
queue.final.pop()(data); //pop the final callbacks and execute, passing data
}
};
self.reject = function(data){ //triggered upon erroneous resolution, this isn't usually directly on the promise object
var i;
while(queue.failure.length > 0){
queue.failure.pop()(data); //pop the failure callback and execute, passing data
}
while(queue.final.length > 0){
queue.final.pop()(data); //pop the final callback and execute, passing data
}
};
self.then = function(fn){
queue.success.push(fn); //push the function
return self; //chainability
};
self.fail = function(fn){
queue.failure.push(fn); //push the function
return self; //chainability
};
self.done = function(fn){
queue.final.push(fn); //push the function
return self; //chainability
};
};
publicMembers.defer = function(){
var promise = new privateMembers.promise(); //create a new promise
return { //return the standard deferral signature, with reject, resolve, and promise
reject: promise.reject, //rejection method binding
resolve: promise.resolve, //resolution method binding
promise: promise //promise object
};
};
}(window)); //invert binding control with closure
var AsyncTask = function(){
var deferral = QLite.defer(); //create a deferral
setTimeout(function(){ //break out of the execution context asynchronously
deferral.resolve("Hello World!"); //resolve the promise and pass "Hello World"
},2000); //execute after 2 seconds, non-blocking
return deferral.promise; //return the promise (this will occur before the above setTimeout executes the resolution)
};
AsyncTask()
.then(function(data){
console.log("FIRST SUCCESS CAKKBACK> "+data);
})
.then(function(data){
console.log("SECOND SUCCESS CALLBACK> "+data);
})
.done(function(data){ //remember, the final stack gets drained AFTER the success stack
console.log("LAST SUCCESS CALLBACK> "+data);
})
.then(function(data){
console.log("THIRD SUCCESS CALLBACK> "+data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment