Last active
June 26, 2019 20:07
Star
You must be signed in to star a gist
A functional Promise implementation, described in https://hackernoon.com/implementing-javascript-promise-in-70-lines-of-code-b3592565af0f
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
const states = { | |
pending: 'Pending', | |
resolved: 'Resolved', | |
rejected: 'Rejected' | |
}; | |
class Nancy { | |
constructor(executor) { | |
const tryCall = callback => Nancy.try(() => callback(this.value)); | |
const laterCalls = []; | |
const callLater = getMember => callback => new Nancy(resolve => laterCalls.push(() => resolve(getMember()(callback)))); | |
const members = { | |
[states.resolved]: { | |
state: states.resolved, | |
then: tryCall, | |
catch: _ => this | |
}, | |
[states.rejected]: { | |
state: states.rejected, | |
then: _ => this, | |
catch: tryCall | |
}, | |
[states.pending]: { | |
state: states.pending, | |
then: callLater(() => this.then), | |
catch: callLater(() => this.catch) | |
} | |
}; | |
const changeState = state => Object.assign(this, members[state]); | |
const apply = (value, state) => { | |
if (this.state === states.pending) { | |
this.value = value; | |
changeState(state); | |
for (const laterCall of laterCalls) { | |
laterCall(); | |
} | |
} | |
}; | |
const getCallback = state => value => { | |
if (value instanceof Nancy && state === states.resolved) { | |
value.then(value => apply(value, states.resolved)); | |
value.catch(value => apply(value, states.rejected)); | |
} else { | |
apply(value, state); | |
} | |
}; | |
const resolve = getCallback(states.resolved); | |
const reject = getCallback(states.rejected); | |
changeState(states.pending); | |
try { | |
executor(resolve, reject); | |
} catch (error) { | |
reject(error); | |
} | |
} | |
static resolve(value) { | |
return new Nancy(resolve => resolve(value)); | |
} | |
static reject(value) { | |
return new Nancy((_, reject) => reject(value)); | |
} | |
static try(callback) { | |
return new Nancy(resolve => resolve(callback())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment