Last active
July 26, 2018 13:55
-
-
Save lgrahl/1852b808273841ed806d9e0baa0aa571 to your computer and use it in GitHub Desktop.
asyncio-like Future for JavaScript
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
/** | |
* A future similar to Python's asyncio.Future. Allows to resolve or reject | |
* outside of the executor. | |
*/ | |
class Future extends Promise { | |
constructor(executor) { | |
let resolve, reject; | |
super((resolve_, reject_) => { | |
resolve = resolve_; | |
reject = reject_; | |
if (executor) { | |
return executor(resolve_, reject_); | |
} | |
}); | |
this._done = false; | |
this._resolve = resolve; | |
this._reject = reject; | |
} | |
get done() { | |
return this._done; | |
} | |
resolve(...args) { | |
this._done = true; | |
return this._resolve(...args); | |
} | |
reject(...args) { | |
this._done = true; | |
return this._reject(...args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment