https://github.com/sergeysova/timeout.js
https://github.com/sergeysova/timeout.js
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/sergeysova/timeout.js
- Owner: sergeysova
- License: wtfpl
- Created: 2016-06-19T13:56:10.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-07-03T12:08:38.000Z (about 9 years ago)
- Last Synced: 2025-03-29T11:06:17.034Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/timeout.js
- Size: 56.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# timeout.js
[](https://github.com/lestad/timeout.js/stargazers)
[](https://github.com/lestad/timeout.js/network)
[](https://npmjs.com/timeout.js)
[]()
[](https://lestad.top)
## Readme
[](https://codeclimate.com/github/LestaD/timeout.js)
[](https://codeclimate.com/github/LestaD/timeout.js/coverage)
[](https://codeclimate.com/github/LestaD/timeout.js)
[](https://travis-ci.org/LestaD/timeout.js)
[]()
Simple promise-based timeout
## Installation
[](https://npmjs.com/timeout.js)
[](https://github.com/LestaD/timeout.js/blob/master/package.json)
[](https://nodei.co/npm/timeout.js/)
```bash
npm install --save timeout.js
```
## Usage
```js
const timeout = require('timeout.js');
// call then callback after 200ms
timeout(200).then(() => console.log('Okey'));
// call catch after .break() called
const exm = timeout(1000);
exm.then(() => console.log('Called!'))
.catch(() => console.log('Breaked!'));
// stop timeout and reject promise
exm.break();
// resolve data after time out
timeout(500, "Data to be resolved")
.then(data => console.log(data));
```
### Async/Await
```js
import timeout from 'timeout.js'
function withData() {
return timeout(300, 'data')
}
async function main() {
await timeout(200)
console.log('After 200 ms')
const data = await withData()
console.log(`After 0.5s with ${data}`)
}
main()
```
### Timeout chaining
```js
const timeout = require('timeout.js');
timeout(300, 'data')
.then(data => timeout(100, data))
.then(timeout.make(100)) // simple
.then(data => {
assert(data === 'data');
})
.catch(error => console.error(error));
// create timeout with predefined time
const out = timeout.make(200);
out().then(() => element.hide());
// with data
const waitFor = timeout.make(500);
waitFor({ user: 123 })
.then(user => request('/user', user))
.then(response => console.log(response.user))
.catch(error => debug(error));
// make timeout with predefined data
const waitData = timeout.make(100, 'data');
waitData()
.then(data => data === 'data');
// override
waitData('foo')
.then(data => data === 'foo');
```
## License
[]()
Copyright © 2016 Sergey Sova
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See the COPYING file or http://www.wtfpl.net/
for more details.