Skip to content

Instantly share code, notes, and snippets.

@lightsofapollo
Created March 14, 2016 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lightsofapollo/069f1586055930396c39 to your computer and use it in GitHub Desktop.
Save lightsofapollo/069f1586055930396c39 to your computer and use it in GitHub Desktop.
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