Skip to content

Instantly share code, notes, and snippets.

@tatat
Created July 5, 2016 07:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tatat/a65a8313ed3d978a84f190c5ac539010 to your computer and use it in GitHub Desktop.
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