README
utils-time
Time-related utilities.
Installation
yarn add @mainframe/utils-time
Usage
import { createWithTimeout, TimeoutError, withTimeout } from 'utils-id'
const max10secs = createWithTimeout(10000)
const run = async () => {
try {
const tooLong = await max10secs(fetch(...))
} catch (err) {
if (err instanceof TimeoutError) {
...
}
}
const longerTime = await withTimeout(fetch(...), 60 * 1000) // 1 min
}
API
sleep
Waits for the given time
before resolving.
Arguments
time: number
Returns Promise<void>
new TimeoutError
Arguments
message: string
withTimeout
Races between the given wait
promise and time
before rejecting with a TimeoutError
.
Arguments
wait: Promise<T>
time: number
Returns Promise<T>
createWithTimeout
Creates a withTimeout()
function with a given default time
.
Arguments
time: number
Returns (wait: Promise<T>, time?: number) => Promise<T>
createGetTime
Creates function that will return an always increasing current time value based on Date.now()
.
Returns () => number
License
MIT