-
-
Save JCloudYu/3f9a16d65e31534f64c0d17503a49f73 to your computer and use it in GitHub Desktop.
How to subclass a promise
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
// ES6 | |
class AngularPromise extends Promise { | |
constructor(executor) { | |
super((resolve, reject) => { | |
// before | |
return executor(resolve, reject); | |
}); | |
// after | |
} | |
then(onFulfilled, onRejected) { | |
// before | |
const returnValue = super.then(onFulfilled, onRejected); | |
// after | |
return returnValue; | |
} | |
} | |
// ES5 | |
function AngularPromise(executor) { | |
var p = new Promise(function (resolve, reject) { | |
// before | |
return executor(resolve, reject); | |
}); | |
// after | |
p.__proto__ = AngularPromise.prototype; | |
return p; | |
} | |
AngularPromise.__proto__ = Promise; | |
AngularPromise.prototype.__proto__ = Promise.prototype; | |
AngularPromise.prototype.then = function then(onFulfilled, onRejected) { | |
// before | |
var returnValue = Promise.prototype.then.call(this, onFulfilled, onRejected); | |
// after | |
return returnValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment