Created
October 26, 2017 22:08
-
-
Save abstractmachines/18157c06b5044148cb34bdcef9b7a4da to your computer and use it in GitHub Desktop.
Dave's pwomise. Remember that the catch is not implemented correctly, recall the tree structure of 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
class Pwomise { | |
constructor (fn) { | |
fn(this._resolve.bind(this), this._reject.bind(this)) | |
this._thens = [] | |
this._catches = [] | |
} | |
_resolve (value) { | |
while (this._thens.length > 0) { | |
this._thens.pop()(value) | |
} | |
} | |
_reject (err) { | |
this._catches.pop()(err) | |
} | |
then (fn) { | |
this._thens.push(fn) | |
} | |
catch (fn) { | |
this._catches.push(fn) | |
} | |
} | |
const p = new Pwomise((resolve, reject) => { | |
setTimeout(() => { | |
resolve('Hello Promises') | |
}, 1000) | |
}) | |
p.then(msg => { | |
console.log(msg) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment