Wait.ts 503 B

1234567891011121314151617181920
  1. class Wait<T = void> {
  2. private _resolve: ((value: T) => void) | null = null;
  3. public wait(): Promise<T> {
  4. return new Promise(resolve => this._resolve = resolve);
  5. }
  6. public resolve(value: T): void {
  7. if (this._resolve) {
  8. this._resolve(value);
  9. this.dispose();
  10. }
  11. }
  12. public dispose():void{
  13. this._resolve=null;
  14. }
  15. }
  16. export default function get_new_wait<T = void>(): Wait<T> {
  17. return new Wait<T>();
  18. }