Skip to content

Commit

Permalink
✨ isAbortError() to support AbortError.timeout()
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Jul 29, 2022
1 parent b255750 commit 85a30e5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,19 @@ export async function pSignal<T>(
}

export function isAbortError(value: unknown): value is Error {
// checking for "TimeoutError" as well because of AbortSignal.timeout(time):
// https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout

if (typeof DOMException === 'undefined') {
return value instanceof Error && value.name === 'AbortError'
return (
value instanceof Error && (value.name === 'AbortError' || value.name === 'TimeoutError')
)
}

return value instanceof DOMException && value.name === 'AbortError'
return (
value instanceof DOMException &&
(value.name === 'AbortError' || value.name === 'TimeoutError')
)
}

function createAbortError(): Error {
Expand Down

0 comments on commit 85a30e5

Please sign in to comment.