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' | |
}; |
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 Nancy { | |
constructor(executor) { | |
const resolve = () => { | |
this.state = states.resolved; | |
}; | |
const reject = () => { | |
this.state = states.rejected; | |
}; |
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 getCallback = state => value => { | |
this.value = value; | |
this.state = state; | |
}; | |
const resolve = getCallback(states.resolved); | |
const reject = getCallback(states.rejected); |
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 Nancy { | |
constructor(executor) { | |
const members = { | |
[states.resolved]: { | |
state: states.resolved, | |
// Chain mechanism | |
then: onResolved => Nancy.resolve(onResolved(this.value)) | |
}, | |
[states.rejected]: { | |
state: states.rejected, |
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 Nancy { | |
... | |
static resolve(value) { | |
return new Nancy(resolve => resolve(value)); | |
} | |
static reject(value) { | |
return new Nancy((_, reject) => reject(value)); | |
} | |
} |
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
let p = new Nancy((resolve, reject) => { | |
resolve(42); | |
reject(24); // ignored | |
resolve(); // ignored | |
}); | |
p | |
.then(value => Nancy.reject(value)) // rejects | |
.catch(value => console.log(value)); // logs 42 |
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 delay = milliseconds => new Nancy(resolve => setTimeout(resolve, milliseconds)); | |
const logThenDelay = milliseconds => total => { | |
console.log(`${total / 1000.0} seconds!`); | |
return delay(milliseconds) | |
.then(() => total + milliseconds); | |
}; | |
logThenDelay(500)(0) // logs 0 seconds! | |
.then(logThenDelay(500)) // after 0.5 seconds, logs 0.5 seconds! | |
.then(logThenDelay(500)) // after 1 second, logs 1 seconds! |
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 Nancy { | |
constructor(executor) { | |
const tryCall = callback => Nancy.try(() => callback(this.value)); | |
const members = { | |
[states.resolved]: { | |
... | |
then: trycall | |
}, | |
... | |
}; |
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 anything = () => { | |
throw new Error('I can be anything because I should never get called!'); | |
}; | |
const throwSomethingWrong = () => { | |
console.log('not ignored!'); | |
throw new Error('Something went wrong...'); | |
}; | |
const p = Nancy.reject(42) | |
.catch(value => value) // resolves |
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
[states.resolved]: { | |
... | |
then: tryCall, | |
catch: _ => this | |
}, | |
[states.rejected]: { | |
... | |
then: _ => this, | |
catch: tryCall | |
} |
OlderNewer