Add isReadyOrError when connecting to API (#2577)

* Add `isReadyOrError`

* lint
This commit is contained in:
Shawn Tabrizi
2020-09-03 17:10:07 +02:00
committed by GitHub
parent 10574ecc33
commit 4654d15097
2 changed files with 17 additions and 3 deletions
+1 -1
View File
@@ -97,7 +97,7 @@ describe('ApiPromise', (): void => {
}
try {
await new ErrorApiPromise().isReady;
await new ErrorApiPromise().isReadyOrError;
fail('Expected an error but none occurred.');
} catch {
// Pass
+16 -2
View File
@@ -197,6 +197,7 @@ export function decorateMethod<Method extends DecorateFn<ObsInnerType<ReturnType
*/
export default class ApiPromise extends ApiBase<'promise'> {
#isReadyPromise: Promise<ApiPromise>;
#isReadyOrErrorPromise: Promise<ApiPromise>;
/**
* @description Creates an ApiPromise instance using the supplied provider. Returns an Promise containing the actual Api instance.
@@ -239,7 +240,13 @@ export default class ApiPromise extends ApiBase<'promise'> {
constructor (options?: ApiOptions) {
super(options, 'promise', decorateMethod);
this.#isReadyPromise = new Promise((resolve, reject): void => {
this.#isReadyPromise = new Promise((resolve): void => {
super.once('ready', (): void => {
resolve(this);
});
});
this.#isReadyOrErrorPromise = new Promise((resolve, reject): void => {
super.once('ready', (): void => {
resolve(this);
});
@@ -250,12 +257,19 @@ export default class ApiPromise extends ApiBase<'promise'> {
}
/**
* @description Promise that returns the first time we are connected and loaded
* @description Promise that resolves the first time we are connected and loaded
*/
public get isReady (): Promise<ApiPromise> {
return this.#isReadyPromise;
}
/**
* @description Promise that resolves if we can connect, or reject if there is an error
*/
public get isReadyOrError (): Promise<ApiPromise> {
return this.#isReadyOrErrorPromise;
}
/**
* @description Returns a clone of this ApiPromise instance (new underlying provider connection)
*/