Last active
September 21, 2018 14:52
-
-
Save carlosrymer/1c5c8ddea75789dae88d3059f0b83cfa to your computer and use it in GitHub Desktop.
Simple ES6 Promise Class
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
// Simulating a Basic Promise in ES6 | |
class Promise { | |
constructor(callback) { | |
this._errorCallbacks = []; | |
this._successCallbacks = []; | |
if (typeof callback === 'function') { | |
try { | |
callback(this.resolveIt.bind(this), this.rejectIt.bind(this)); | |
} catch (e) { | |
this.rejectIt(e); | |
} | |
} | |
return this; | |
} | |
catch(callback) { | |
this._errorCallbacks.push((...args) => { | |
let result; | |
try { | |
result = callback.apply(this, args); | |
} catch (e) { | |
return this.rejectIt(e); | |
} | |
return this.resolveIt(result); | |
}); | |
return this; | |
} | |
rejectIt(error) { | |
let result; | |
let callback; | |
if (typeof this._errorCallbacks[0] === 'function') { | |
callback = this._errorCallbacks.shift(); | |
try { | |
result = callback.call(this, error); | |
} catch (e) { | |
this.rejectIt(e); | |
} | |
} else { | |
throw error; | |
} | |
} | |
resolveIt(value) { | |
let result; | |
let callback; | |
if (typeof this._successCallbacks[0] === 'function') { | |
callback = this._successCallbacks.shift(); | |
try { | |
result = callback.call(this, value); | |
} catch (e) { | |
this.rejectIt(e); | |
} | |
} | |
return value; | |
} | |
static reject(error) { | |
return new Promise((resolve, reject) => { | |
reject(error); | |
}); | |
} | |
static resolve(result) { | |
return new Promise(resolve => { | |
resolve(result); | |
}); | |
} | |
then(callback) { | |
this._successCallbacks.push((...args) => { | |
let result; | |
try { | |
result = callback.apply(this, args); | |
} catch (e) { | |
return this.rejectIt(e); | |
} | |
return this.resolveIt(result); | |
}); | |
return this; | |
} | |
} | |
// Usage | |
new Promise((resolve, reject) => setTimeout(() => resolve('OK'), 1000)) | |
.then(result => { | |
console.log(result); | |
return result; | |
}) | |
.then(result => { | |
console.log(result); | |
return result; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment