Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jpwilliams/primeout
Create a promise with a timeout.
https://github.com/jpwilliams/primeout
Last synced: 22 days ago
JSON representation
Create a promise with a timeout.
- Host: GitHub
- URL: https://github.com/jpwilliams/primeout
- Owner: jpwilliams
- License: mit
- Created: 2017-01-03T15:50:25.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-09T13:14:40.000Z (almost 8 years ago)
- Last Synced: 2024-09-19T16:10:56.629Z (about 2 months ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# _Primeout_
[![Build Status](https://travis-ci.org/jpwilliams/primeout.svg)](https://travis-ci.org/jpwilliams/primeout) [![Coverage Status](https://coveralls.io/repos/github/jpwilliams/primeout/badge.svg)](https://coveralls.io/github/jpwilliams/primeout) [![npm downloads per month](https://img.shields.io/npm/dm/primeout.svg)](https://www.npmjs.com/package/primeout) [![npm version](https://img.shields.io/npm/v/primeout.svg)](https://www.npmjs.com/package/primeout)
Create a promise with a timeout.
``` js
npm install primeout
`````` js
const primeout = require('primeout')// Will reject if the `connectToDb()` promise does
// not respond within 30 seconds.
primeout('30 seconds', connectToDb()).then((db) => {
...
}).catch((err) => {
...
})
```## Contents
* [Examples](#examples)
* [API reference](#api-reference)## Examples
All of these examples assume that `connect()` returns a promise.
``` js
// Set a timeout of 5 seconds
primeout(5000, connect())
.then(...)
.catch(...)
`````` js
// Set a timeout of 5 minutes
primeout('5m', connect())
.then(...)
.catch(...)
`````` js
// Set a timeout that rejects if the
// promise is not resolved or rejected
// before the next event loop.
primeout(0, connect())
.then(...)
.catch(...)
```For a clear-cut explanation of that last example (especially if you're new to _node.js_) see [Understanding the node.js event loop](http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/).
## API reference
#### `primeout`
The only export from the module. A function used to return the timeout-enabled promise desired.
##### Arguments
* `time` The timeout being specified. Can either be a number specifying the amount of milliseconds or a valid [timestring](https://www.npmjs.com/package/timestring).
* `promise` The promise you wish to set a timeout for.