promto

promto
Wrappes A Promise In Timeout.
yarn add promto
Table Of Contents
API
The package is available by importing its default function:
import promto from 'promto'
async promto(
promise: Promise,
timeout: number,
description: string?,
): Promise
Creates a new promise which will be rejected upon timeout (after N milliseconds). It will be resolved with the promise value or rejected with the promise error when not timed out.
import promto from 'promto'
const makePromise = async (timeout, result) => {
await new Promise((resolve) => {
setTimeout(resolve, timeout || 200)
})
if (result instanceof Error) throw result
return result
}
(async () => {
const res = await promto(makePromise(50, 'hello'), 100)
console.log(res)
try {
await promto(makePromise(150, 'world'), 100)
} catch (err) {
console.log(err.message)
}
try {
await promto(makePromise(150, 'world'), 100, 'Example')
} catch (err) {
console.log(err.message)
}
try {
await promto(makePromise(50, new Error('Error in promise')), 100)
} catch (err) {
console.log(err.message)
}
})()
hello
Promise has timed out after 100ms
Example has timed out after 100ms
Error in promise
Copyright