sleepWithCancel
object Object
Usage
sleepWithCancel creates a cancellable delay, returning both a promise and a cancel function.
import { sleepWithCancel } from '@movk/core'
const { promise, cancel } = sleepWithCancel(5000)
// Cancel the delay from elsewhere
setTimeout(() => {
cancel() // Abort the delay
}, 2000)
try {
await promise
console.log('Executed after 5 seconds')
}
catch (error) {
console.log('Delay was cancelled')
}
API
sleepWithCancel(ms)
Return a cancellable delay: a promise and a cancel function.
Parameters
ms
number required
The delay duration in milliseconds.
Returns
returns
{ promise: Promise<void>; cancel: () => void }
An object containing the delay promise and a cancel function.
Changelog
No recent changes