Skip to content

Instantly share code, notes, and snippets.

@chrisweight
Last active October 17, 2018 09:35
Show Gist options
  • Save chrisweight/22cca6c73cbeb190e8dd7af1bf1e2bf5 to your computer and use it in GitHub Desktop.
Save chrisweight/22cca6c73cbeb190e8dd7af1bf1e2bf5 to your computer and use it in GitHub Desktop.
Ready Resolver Pattern
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;
}
}
@chrisweight
Copy link
Author

Means you can succinctly wait for init to complete, i.e.:

const someClass = new SomeAsyncClass();
someClass.ready().then(() => {
// crack on here
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment