https://github.com/75lb/race-timeout-anywhere
An isomorphic, load-anywhere timeout function for use with Promise.race.
https://github.com/75lb/race-timeout-anywhere
Last synced: 3 months ago
JSON representation
An isomorphic, load-anywhere timeout function for use with Promise.race.
- Host: GitHub
- URL: https://github.com/75lb/race-timeout-anywhere
- Owner: 75lb
- License: mit
- Created: 2018-09-26T20:39:52.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-06-06T23:18:56.000Z (about 5 years ago)
- Last Synced: 2024-04-25T08:01:25.896Z (about 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.org/package/race-timeout-anywhere)
[](https://www.npmjs.org/package/race-timeout-anywhere)
[](https://github.com/75lb/race-timeout-anywhere/network/dependents?dependent_type=REPOSITORY)
[](https://github.com/75lb/race-timeout-anywhere/network/dependents?dependent_type=PACKAGE)
[](https://travis-ci.org/75lb/race-timeout-anywhere)
[](https://github.com/feross/standard)
# race-timeout-anywhere
An isomorphic, load-anywhere timeout function for use with `Promise.race`.
The `raceTimeout` function never resolves, it will only reject once the specified time period has elapsed. In the example below, `fetchDataSomehow()` must resolve before `raceTimeout` rejects after 1000ms, else a timeout exception is thrown.
```js
import raceTimeout from 'race-timeout-anywhere'
try {
const data = await Promise.race([
fetchDataSomehow(),
raceTimeout(1000)
])
/* fetchDataSomehow() took less than 1000ms, process data */
} catch (err) {
/* fetchDataSomehow() took longer than 1000ms */
}
```
### Load anywhere
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
CommonJS:
```js
const raceTimeout = require('race-timeout-anywhere')
```
ECMAScript Module:
```js
import raceTimeout from 'rice-timeout-anywhere'
```
Within a modern browser ECMAScript Module:
```js
import raceTimeout from './node_modules/rice-timeout-anywhere/index.mjs'
```
* * *
© 2018-21 Lloyd Brookes \<75pound@gmail.com\>.