Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

await-timeout

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

await-timeout - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

dist/es5.js

16

package.json
{
"name": "await-timeout",
"version": "0.5.0",
"version": "0.6.0",
"description": "A Promise-based API for setTimeout / clearTimeout",

@@ -16,10 +16,12 @@ "author": {

},
"main": "dist/bundle.umd.js",
"main": "dist/es6.js",
"scripts": {
"code": "eslint src test scripts rollup.config.js",
"build": "rollup -c",
"test": "npm run build && mocha test/setup test/specs",
"test-installed": "node scripts/install-local && LIB_PATH=../.installed/node_modules/await-timeout npm test",
"test": "npm run build && npm run test-lib",
"test-lib": "mocha test/setup test/specs",
"test-es5": "LIB_PATH=../dist/es5 npm run test-lib",
"test-installed": "node scripts/install-local && LIB_PATH=../.installed/node_modules/await-timeout npm run test-lib",
"ci": "npm run code && npm test",
"prerelease": "npm run code && npm test && npm run test-installed",
"prerelease": "npm run code && npm test && npm run test-es5 && npm run test-installed",
"release": "npm version $VER && npm publish",

@@ -45,2 +47,4 @@ "postrelease": "git push --follow-tags --no-verify",

"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"assert-rejects": "^1.0.0",

@@ -53,3 +57,3 @@ "eslint": "^5.16.0",

"rollup": "^1.11.3",
"rollup-plugin-banner": "^0.2.0",
"rollup-plugin-babel": "^4.3.3",
"sinon": "^7.3.2"

@@ -56,0 +60,0 @@ },

@@ -39,4 +39,4 @@ <div align="center">

// wait 1000 ms and reject with 'Error'
await Timeout.set(1000, 'Error');
// wait 1000 ms and reject with 'Timeout!'
await Timeout.set(1000, 'Timeout!');
```

@@ -52,4 +52,3 @@

fetch('https://example.com'),
timer.set(1000)
.then(() => Promise.reject('Timeout'))
timer.set(1000, 'Timeout!')
]);

@@ -60,3 +59,13 @@ } finally {

```
> Without a timer cleanup you may get unexpected effects in you code - as all promises in `Promise.race`
> are get fulfilled.
#### Usage in ES5
If you are targeting ES5 environment please use transpiled build from `await-timeout/dist/es5`:
```js
import Timeout from 'await-timeout/dist/es5';
...
```
## API

@@ -68,3 +77,3 @@ ### new Timeout()

```
> Note: having separate variable is useful for clearing timeout in `finally` block
> Note: having separate `timer` variable is useful for clearing timeout in `finally` block

@@ -79,20 +88,11 @@ ### .set(delay, [rejectReason]) ⇒ `Promise`

If you need to reject after timeout:
If you provide `rejectReason` - a timer promise will be rejected with specified reason:
```js
timer.set(1000)
.then(() => {throw new Error('Timeout')});
// rejects with Error: Timeout after 1000 ms:
timer.set(1000, 'Timeout after 1000 ms');
// above is actually shortcut for:
timer.set(1000).then(() => Promise.reject(new Error('Timeout after 1000 ms')));
```
Or reject with custom error:
```js
timer.set(1000)
.then(() => {throw new MyTimeoutError()});
```
The second parameter `message` is just convenient way to reject with `new Error(message)`:
```js
timer.set(1000, 'Timeout');
// is equivalent to
timer.set(1000).then(() => {throw new Error('Timeout')});
```
If you need to just wait some time - use static version of `.set()`:

@@ -106,20 +106,22 @@ ```js

* promise automatically rejected after timeout
* timeout automatically cleared if promise fulfills first
* timeout automatically cleared if main promise resolves first
```js
const promise = fetch('https://example.com');
const timeoutedPromise = Timeout.wrap(promise, 1000, 'Timeout');
async function fetchWithTimeout() {
const promise = fetch('https://example.com');
return Timeout.wrap(promise, 1000, 'Timeout');
}
```
Actually it is a shortcut for:
```js
const promise = fetch('https://example.com');
const timer = new Timeout();
try {
const timeoutedPromise = await Promise.race([
promise,
timer.set(1000, 'Timeout')
]);
} finally {
timer.clear();
async function fetchWithTimeout() {
const timer = new Timeout();
try {
const promise = fetch('https://example.com');
return await Promise.race([
promise,
timer.set(1000, 'Timeout')
]);
} finally {
timer.clear();
}
}

@@ -160,9 +162,8 @@ ```

## Motivation
Before making this library I've researched [many similar packages on Npm](https://www.npmjs.com/search?q=promise%20timeout).
Before making this library I've researched [several similar packages on Npm](https://www.npmjs.com/search?q=promise%20timeout).
But no one satisfied all my needs together:
1. Convenient way to cancel timeout. I typically use it with [Promise.race()] and don't want timer to trigger
if main promise is fulfilled first.
if main promise is resolved first.
2. API similar to `setTimeout` / `clearTimeout`. I get used to these functions and would like to have mirror syntax.

@@ -169,0 +170,0 @@ 3. Easy rejection of timeout promise. Passing error message should be enough.

@@ -1,13 +0,39 @@

import banner from 'rollup-plugin-banner';
import babel from 'rollup-plugin-babel';
import pkg from './package.json';
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.umd.js',
name: 'Timeout',
format: 'umd'
},
plugins: [
banner('<%= pkg.name %> v<%= pkg.version %> by <%= pkg.author.name %>')
]
const createBaseConfig = () => {
return {
input: 'src/index.js',
output: {
file: '',
name: 'Timeout',
format: 'umd',
banner: `/* ${pkg.name} v${pkg.version} by ${pkg.author.name} */`
},
plugins: []
};
};
const createES6Config = () => {
const config = createBaseConfig();
config.output.file = 'dist/es6.js';
return config;
};
const createES5Config = () => {
const config = createBaseConfig();
config.output.file = 'dist/es5.js';
config.plugins.push(
babel({
babelrc: false,
presets: [['@babel/env', { modules: false }]],
exclude: 'node_modules/**'
}),
);
return config;
};
export default [
createES6Config(),
createES5Config(),
];
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc