Last active
October 17, 2018 09:35
-
-
Save chrisweight/22cca6c73cbeb190e8dd7af1bf1e2bf5 to your computer and use it in GitHub Desktop.
Ready Resolver Pattern
This file contains hidden or 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
export class SomeAsyncClass { | |
private _ready: Promise<void>; | |
private _readyResolver: Function; | |
constructor() { | |
this._ready = new Promise(resolve => (this._readyResolver = resolve)); | |
this.init(); | |
} | |
private async init() { | |
// Do some async bootstrapping here, then... | |
this._readyResolver(); | |
} | |
public async ready(): Promise<void> { | |
return this._ready; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Means you can succinctly wait for init to complete, i.e.: