Skip to content

Instantly share code, notes, and snippets.

@marksteward
Last active March 14, 2021 03:10
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 marksteward/de8cbe2a055f4cfcdc150cba05800639 to your computer and use it in GitHub Desktop.
Save marksteward/de8cbe2a055f4cfcdc150cba05800639 to your computer and use it in GitHub Desktop.
Is this evil?
class StateWaiter extends Promise {
constructor(resolveStates, rejectStates) {
let handlers;
super((resolve, reject) => handlers = {resolve, reject});
this._handlers = handlers;
this._resolveStates = resolveStates;
this._rejectStates = rejectStates;
}
check(state) {
if (this._resolveStates.includes(state)) this._handlers.resolve(state);
if (this._rejectStates.includes(state)) this._handlers.reject(state);
}
static [Symbol.species] = Promise;
[Symbol.toStringTag] = 'StateWaiter';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment