Skip to content

Instantly share code, notes, and snippets.

@jasonwaters
Created February 16, 2017 21:30
Show Gist options
  • Save jasonwaters/f928eb6cdac1e1f4bcda783529f2d88b to your computer and use it in GitHub Desktop.
Save jasonwaters/f928eb6cdac1e1f4bcda783529f2d88b to your computer and use it in GitHub Desktop.
A promise implementation
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