Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@abstractmachines
Created October 26, 2017 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abstractmachines/18157c06b5044148cb34bdcef9b7a4da to your computer and use it in GitHub Desktop.
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.
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