Skip to content

Instantly share code, notes, and snippets.

@kitak
Last active April 24, 2017 11:46
Show Gist options
  • Save kitak/776598b462f2fbd0d2ac53d5a56b92b2 to your computer and use it in GitHub Desktop.
Save kitak/776598b462f2fbd0d2ac53d5a56b92b2 to your computer and use it in GitHub Desktop.
class Checker {
start() {
if (this.checked) {
this.stop();
}
this.checked = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
_clean() {
this.checked = null;
this.resolve = null;
this.reject = null;
}
done() {
if (this.resolve) {
this.resolve();
this._clean();
}
}
stop() {
if (this.reject) {
this.reject();
this._clean();
}
}
}
const checker = new Checker();
checker.start()
const checked = checker.checked
checked.then(() => {
console.log('yatta!');
}).catch(() => {
console.log('dameda!');
});
setTimeout(() => {
let checked2 = checker.start();
console.log(checked === checked2);
checker.done();
checker.stop();
}, 2000);
// というか、これは deferred https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment