Created
March 14, 2016 23:06
-
-
Save lightsofapollo/069f1586055930396c39 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 EventWaiter { | |
_emitter: EventEmitter; | |
_event: string; | |
_promise: Promise<mixed>; | |
_accept: Function; | |
_onEvent = (value) => { | |
this._accept(value); | |
this._emitter.removeEventListener(this._event, this._onEvent); | |
}; | |
constructor(emitter: EventEmitter, event: string) { | |
this._emitter = emitter; | |
this._event = event; | |
this._promise = new Promise((accept) => { | |
this._accept = accept; | |
emitter.on(event, this._onEvent); | |
}); | |
} | |
await(): Promise { | |
return this._promise; | |
} | |
release() { | |
this._emitter.removeEventListener(this._event, this._onEvent); | |
this._accept(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment