Last active
April 24, 2017 11:46
-
-
Save kitak/776598b462f2fbd0d2ac53d5a56b92b2 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 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