Created
March 30, 2011 13:19
-
-
Save briancavalier/894360 to your computer and use it in GitHub Desktop.
Promise chaining implementation of then()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise.prototype.then = function (onResolve, onReject, onProgress) { | |
if(isPromise(onResolve)) { | |
// Chain promise | |
this.then( | |
function(val) { onResolve.resolve(onReject ? onReject : val); }, | |
function(err) { onResolve.reject(err); }, | |
function(update) { onResolve.progress(update); } | |
); | |
} else { | |
// capture calls to then() | |
this._thens.push({ resolve: onResolve, reject: onReject, progress: onProgress }); | |
onProgress && this._progress.push(onProgress); | |
} | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment