Created
July 5, 2016 07:49
-
-
Save tatat/a65a8313ed3d978a84f190c5ac539010 to your computer and use it in GitHub Desktop.
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 F extends Promise { | |
onComplete(callback) { | |
this.then(callback, callback); | |
} | |
} | |
class P { | |
constructor() { | |
this.future = new F((resolve, reject) => { | |
this.trySuccess = resolve; | |
this.tryFailure = reject; | |
}); | |
} | |
onSuccess(callback) { | |
this.future.then(callback); | |
} | |
onFailure(callback) { | |
this.future.catch(callback); | |
} | |
} | |
const p = new P(); | |
p.onSuccess((...args) => console.log(1, args)); | |
p.onFailure((...args) => console.log(2, args)); | |
p.future.onComplete((...args) => console.log(3, args)); | |
setTimeout(() => { | |
p.trySuccess('Succeeded!'); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment