Simple Javascript Promise Implementation (see this blog post: http://www.opensourceconnections.com/2014/02/16/a-simple-promise-implementation-in-about-20-lines-of-javascript/)
var Promise = function(wrappedFn, wrappedThis) { | |
this.then = function(wrappedFn, wrappedThis) { | |
this.next = new Promise(wrappedFn, wrappedThis); | |
return this.next; | |
}; | |
this.run = function() { | |
wrappedFn.promise = this; | |
wrappedFn.apply(wrappedThis); | |
}; | |
this.complete = function() { | |
if (this.next) { | |
this.next.run(); | |
} | |
}; | |
}; | |
Promise.create = function(func) { | |
if (func.hasOwnProperty('promise')) { | |
return func.promise; | |
} else { | |
return new Promise(); | |
} | |
}; |
This comment has been minimized.
This comment has been minimized.
Doesnt work. Can you please provide a full example?
Also doesnt work:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
sample usage
var foo = function() {
var promise = Promise.create(foo)
async.wait(function() {
promise.complete();
});
return promise;
}