Skip to content

Instantly share code, notes, and snippets.

@JasonKleban
Last active March 27, 2019 11:53
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 JasonKleban/0c0b4fefe7553c4da41a6770bda3864b to your computer and use it in GitHub Desktop.
Save JasonKleban/0c0b4fefe7553c4da41a6770bda3864b to your computer and use it in GitHub Desktop.
Convert events into Promises to be awaited
export interface LockStep<T = void> {
edge : Promise<T>;
}
export class LockStepper<T = void> implements LockStep<T> {
private _edge! : Promise<T>;
private _resolve! : (value?: T | PromiseLike<T> | undefined) => void;
constructor () {
this.rearm();
}
private rearm() {
this._edge = new Promise<T>((resolve) => {
this._resolve = resolve;
});
}
get edge() {
return this._edge;
}
expose() {
return this as LockStep<T>;
}
step(value?: T | PromiseLike<T> | undefined) {
const _resolve = this._resolve;
this.rearm();
_resolve(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment