-
-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix abortions between retries (#433)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
6d9f7c8
commit dddf7ba
Showing
7 changed files
with
60 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const DOMException = globalThis.DOMException ?? Error; | ||
|
||
export default DOMException; | ||
|
||
// When targeting Node.js 18, use `signal.throwIfAborted()` (https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/throwIfAborted) | ||
export function composeAbortError(signal?: AbortSignal) { | ||
return new DOMException(signal?.reason ?? 'The operation was aborted.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// https://github.com/sindresorhus/delay/tree/ab98ae8dfcb38e1593286c94d934e70d14a4e111 | ||
|
||
import {composeAbortError} from '../errors/DOMException.js'; | ||
import {InternalOptions} from '../types/options.js'; | ||
|
||
export interface DelayOptions { | ||
signal?: InternalOptions['signal']; | ||
} | ||
|
||
export default async function delay( | ||
ms: number, | ||
{signal}: DelayOptions, | ||
): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
if (signal) { | ||
if (signal.aborted) { | ||
reject(composeAbortError(signal)); | ||
return; | ||
} | ||
|
||
signal.addEventListener('abort', handleAbort, {once: true}); | ||
} | ||
|
||
function handleAbort() { | ||
reject(composeAbortError(signal!)); | ||
clearTimeout(timeoutId); | ||
} | ||
|
||
const timeoutId = setTimeout(() => { | ||
signal?.removeEventListener('abort', handleAbort); | ||
resolve(); | ||
}, ms); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters