Created
February 16, 2017 21:30
-
-
Save jasonwaters/f928eb6cdac1e1f4bcda783529f2d88b to your computer and use it in GitHub Desktop.
A promise implementation
This file contains hidden or 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
| const noop = () => void 0; | |
| function Covenant(fn) { | |
| this.resolver = fn; | |
| this.thenCallback = noop; | |
| this.catchCallback = noop; | |
| this.resolver((...args) => { | |
| this.thenCallback(...args); | |
| }, () => { | |
| this.thenCallback = noop; | |
| this.catchCallback('error'); | |
| }); | |
| } | |
| Covenant.prototype.then = function(fn) { | |
| this.thenCallback = fn; | |
| return this; | |
| }; | |
| Covenant.prototype.catch = function(fn) { | |
| this.catchCallback = fn; | |
| return this; | |
| }; | |
| let c = new Covenant((resolve, reject) => { | |
| // setTimeout(() => reject(), 500); | |
| setTimeout(() => resolve('woot'), 1000); | |
| }); | |
| c.then((something) => { | |
| console.log(`${something}, done!`); | |
| }).catch(() => { | |
| console.log('oh shat'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment