Make sure awaiting on an api object won't stuck forever on error (#1763)
* Make sure awaiting on an api object won't stuck forever when error occurs. * Fix lint issues.
This commit is contained in:
@@ -60,6 +60,25 @@ describe('ApiPromise', (): void => {
|
||||
expect(api.tx).toBeDefined();
|
||||
expect(api.derive).toBeDefined();
|
||||
});
|
||||
|
||||
it('Create API instance will error on failure to await ready', async (): Promise<void> => {
|
||||
class ErrorApiPromise extends ApiPromise {
|
||||
constructor () {
|
||||
super({ provider });
|
||||
}
|
||||
|
||||
protected loadMeta (): Promise<boolean> {
|
||||
throw new Error('Simulate failure to load meta');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await new ErrorApiPromise().isReady;
|
||||
fail('Expected an error but none occurred.');
|
||||
} catch {
|
||||
// Pass
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('api.sign', (): void => {
|
||||
|
||||
@@ -216,10 +216,13 @@ export default class ApiPromise extends ApiBase<'promise'> {
|
||||
constructor (options?: ApiOptions) {
|
||||
super(options, 'promise', decorateMethod);
|
||||
|
||||
this._isReadyPromise = new Promise((resolve): void => {
|
||||
this._isReadyPromise = new Promise((resolve, reject): void => {
|
||||
super.once('ready', (): void => {
|
||||
resolve(this);
|
||||
});
|
||||
super.once('error', (e): void => {
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user