@cdellacqua/sleep
Ƭ SleepConfig<T
>: Object
A configuration object for the sleep
function.
Name | Type |
---|---|
T |
extends TimeoutIdentifier |
Name | Type | Description |
---|---|---|
signal? |
AbortSignal |
An optional AbortSignal that can be used to cancel (by rejecting with the abort reason) the sleep promise before its natural termination. |
timeoutApi? |
TimeoutAPI <T > |
A custom timeout API that provides setTimeout and clearTimeout. |
Ƭ TimeoutAPI<T
>: Object
A basic timeout API must provide a setTimeout and a clearTimeout function.
Name | Type |
---|---|
T |
extends TimeoutIdentifier |
Name | Type |
---|---|
clearTimeout |
(timeoutId : T ) => void |
setTimeout |
(callback : () => void , ms : number ) => T |
Ƭ TimeoutIdentifier: string
| number
| boolean
| symbol
| bigint
| object
| null
Any viable type that may be used by a setTimeout implementation.
▸ sleep<T
>(ms
, config
): Promise
<void
>
Return a Promise that resolves after the specified delay.
The second parameter is a config
object that can contain a custom timeout API,
providing setTimeout and clearTimeout functions,
and an AbortSignal that can be used
to cancel (by rejecting with the abort reason) the sleep promise before its natural termination.
Overriding the timeout API can be useful in tests or in scenarios where you would want to use more precise timing than what setTimeout can offer.
Note: if the delay is 0 the returned Promise will be already resolved.
Name | Type |
---|---|
T |
extends TimeoutIdentifier |
Name | Type | Description |
---|---|---|
ms |
number |
a delay in milliseconds. |
config |
SleepConfig <T > |
an object containing a signal and custom timeout API providing setTimeout and clearTimeout functions. |
Promise
<void
>
a Promise
▸ sleep(ms
): Promise
<void
>
Return a Promise that resolves after the specified delay.
Note: if the delay is 0 the returned Promise will be already resolved.
Name | Type | Description |
---|---|---|
ms |
number |
a delay in milliseconds. |
Promise
<void
>
a Promise