public
Created

Promise chaining implementation of then()

  • Download Gist
then-promise.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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;
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.