https://github.com/artdecocode/promto
Promise with timeout
https://github.com/artdecocode/promto
Last synced: about 2 months ago
JSON representation
Promise with timeout
- Host: GitHub
- URL: https://github.com/artdecocode/promto
- Owner: artdecocode
- License: mit
- Created: 2017-05-14T19:50:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-24T22:20:26.000Z (about 6 years ago)
- Last Synced: 2025-09-21T13:57:31.483Z (4 months ago)
- Language: JavaScript
- Size: 23.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# promto
[](https://www.npmjs.com/package/promto)
`promto` Wrappes A Promise In Timeout.
```sh
yarn add promto
```
## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [API](#api)
- [`async promto(promise: Promise, timeout: number, description: string?): Promise`](#async-promtopromise-promisetimeout-numberdescription-string-promise)
- [Copyright](#copyright)
## API
The package is available by importing its default function:
```js
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.
```js
import promto from 'promto'
/**
* @param {number} timeout
* @param {string|Error} result
*/
const makePromise = async (timeout, result) => {
await new Promise((resolve) => {
setTimeout(resolve, timeout || 200)
})
if (result instanceof Error) throw result
return result
}
(async () => {
// ok
const res = await promto(makePromise(50, 'hello'), 100)
console.log(res)
// rejected timeout
try {
await promto(makePromise(150, 'world'), 100)
} catch (err) {
console.log(err.message)
}
// rejected timeout with description
try {
await promto(makePromise(150, 'world'), 100, 'Example')
} catch (err) {
console.log(err.message)
}
// rejected promise
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
© Art Deco 2019
Tech Nation Visa Sucks