Add isReadyOrError when connecting to API (#2577)
* Add `isReadyOrError` * lint
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user