promise-assist
Several helper functions when working with native promises.
API
sleep
Useful for waiting a specific amount of time before continuing an operation.
import { sleep } from 'promise-assist'
async function myOperation() {
const startTime = Date.now()
await sleep(500)
console.log(`${Date.now() - startTime}ms passed!`)
}
timeout
Useful for limiting the amount of time an async Promise-based operation can take.
import { timeout } from 'promise-assist'
async function myOperation() {
try {
const data = await timeout(
fetchDataFromServer(),
10000,
`failed loading required data from backend`
)
} catch (e) {
}
}
deferred
Creates a deferred Promise, where resolve
/reject
are exposed to the place that holds the promise.
Generally bad practice, but there are use-cases where one mixes callback-based API with Promise API and this is helpful.
import { deferred } from 'promise-assist'
const { promise, resolve, reject } = deferred<string>()
promise.then(value => console.log(value))
resolve('some text')
License
MIT