Skip to content

Commit

Permalink
feat(esl-utils): add ability to reject promisifyEvent by using AbortS…
Browse files Browse the repository at this point in the history
…ignal
  • Loading branch information
dshovchko committed May 7, 2024
1 parent 35f968c commit b49da0e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/modules/esl-utils/async/promise/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export function promisifyEvent(
if (typeof timeout === 'number' && timeout >= 0) {
setTimeout(eventCallback, timeout);
}
if (typeof options === 'object') {
options?.signal?.addEventListener('abort', () => reject(new Error('Rejected by abort signal')), {once: true});
}
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/modules/esl-utils/async/test/promise/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ describe('async/promise/event', () => {
expect(spy).toBeCalledWith('test', expect.any(Function), undefined);
}
});
test('Rejected by abort signal', async () => {
const el = document.createElement('div');
const controller = new AbortController();
const promise$ = promisifyEvent(el, 'test', null, {signal: controller.signal});
controller.abort();
await expect(promise$).rejects.toBeInstanceOf(Error);
});
});

describe('promisifyMarker', () => {
Expand Down

0 comments on commit b49da0e

Please sign in to comment.