Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Created March 30, 2011 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briancavalier/894360 to your computer and use it in GitHub Desktop.
Save briancavalier/894360 to your computer and use it in GitHub Desktop.
Promise chaining implementation of then()
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