Skip to content

Commit

Permalink
perf: customError name
Browse files Browse the repository at this point in the history
  • Loading branch information
bowencool committed Mar 25, 2022
1 parent 64c2b99 commit 9a87f0f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/abortableAsync/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
signal: controller.signal,
})(1900 + Math.floor(Math.random() * 200));
} catch (error: any) {
console.log(error.name, error.message, error instanceof TimeoutError, error instanceof AbortError);
console.log(error.name, error instanceof TimeoutError, error instanceof AbortError);
state.error = error;
state.data = '';
} finally {
Expand Down
2 changes: 2 additions & 0 deletions packages/abortableAsync/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('abortableAsync', () => {
const p2 = fn(2000);
jest.advanceTimersByTime(2000);
await expect(p2).rejects.toBeInstanceOf(TimeoutError);
await expect(p2).rejects.toMatchObject({ name: 'TimeoutError' });
});
test('signal', async () => {
// controller.abort() 只能触发一次 abort 事件
Expand All @@ -48,5 +49,6 @@ describe('abortableAsync', () => {
jest.advanceTimersByTime(1000);
controller.abort();
await expect(p2).rejects.toBeInstanceOf(AbortError);
await expect(p2).rejects.toMatchObject({ name: 'AbortError' });
});
});
16 changes: 4 additions & 12 deletions packages/abortableAsync/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
export class AbortError extends Error {
constructor(message: string) {
super(message);
this.name = 'AbortError';
}
}
export class TimeoutError extends Error {
constructor(message: string) {
super(message);
this.name = 'TimeoutError';
}
}
export class AbortError extends Error {}
AbortError.prototype.name = 'AbortError';
export class TimeoutError extends Error {}
TimeoutError.prototype.name = 'TimeoutError';

export function abortableAsync<T, P extends any[], R>(
fn: (this: T, ...p: P) => Promise<R>,
Expand Down

0 comments on commit 9a87f0f

Please sign in to comment.