await-timeout
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -5,3 +5,3 @@ /** | ||
export default class Timeout { | ||
class Timeout { | ||
constructor() { | ||
@@ -22,1 +22,5 @@ this._id = null; | ||
} | ||
Timeout.wait = ms => new Timeout().set(ms); | ||
export default Timeout; |
@@ -1,2 +0,2 @@ | ||
/*! await-timeout v0.1.0 */ | ||
/*! await-timeout v0.1.1 */ | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
@@ -127,2 +127,6 @@ if(typeof exports === 'object' && typeof module === 'object') | ||
Timeout.wait = function (ms) { | ||
return new Timeout().set(ms); | ||
}; | ||
exports.default = Timeout; | ||
@@ -129,0 +133,0 @@ module.exports = exports['default']; |
{ | ||
"name": "await-timeout", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A Promise-based API for setTimeout / clearTimeout", | ||
@@ -18,10 +18,8 @@ "author": { | ||
"scripts": { | ||
"code": "eslint index.js test.js webpack.config.js", | ||
"code": "eslint index.js webpack.config.js test scripts", | ||
"build": "webpack", | ||
"test": "mocha test.js -r babel-register", | ||
"test-umd": "npm run build && LIB_PATH=. mocha test.js", | ||
"test-installed": "npm run install-local && LIB_PATH=.installed/node_modules/await-timeout npm t", | ||
"test": "mocha test/specs -r test/setup", | ||
"test-umd": "npm run build && LIB_PATH=.. npm t", | ||
"test-installed": "node scripts/install-local && LIB_PATH=../.installed/node_modules/await-timeout npm t", | ||
"ci": "npm run code && npm test && npm run test-umd", | ||
"install-local": "npm run make-installed-dir && pushd .installed && install-local .. && popd", | ||
"make-installed-dir": "mkdir -p .installed && echo '{\"name\": \"dummy\"}' > .installed/package.json", | ||
"prerelease": "npm run code && npm test && npm run test-umd", | ||
@@ -35,8 +33,2 @@ "release": "npm version $VER && npm publish", | ||
}, | ||
"babel": { | ||
"plugins": [ | ||
"add-module-exports", | ||
"transform-es2015-modules-commonjs" | ||
] | ||
}, | ||
"devDependencies": { | ||
@@ -43,0 +35,0 @@ "babel-cli": "^6.24.1", |
@@ -1,18 +0,19 @@ | ||
<p align="center"> | ||
<div align="center"> | ||
<img src="https://user-images.githubusercontent.com/1473072/32229482-f90f07d2-be61-11e7-86f1-f9f555182292.png"> | ||
<h1 align="center">await-timeout</h1> | ||
</p> | ||
</div> | ||
<h1 align="center">await-timeout</h1> | ||
<h5 align="center">A <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>-based API for setTimeout / clearTimeout</h5> | ||
<div align="center"> | ||
<a href="https://travis-ci.org/vitalets/await-timeout"><img src="https://travis-ci.org/vitalets/await-timeout.svg?branch=master" alt="Build Status" /></a> | ||
<a href="https://www.npmjs.com/package/await-timeout"><img src="https://img.shields.io/npm/v/await-timeout.svg" alt="Npm version" /></a> | ||
<a href="https://www.npmjs.com/package/await-timeout"><img src="https://img.shields.io/npm/l/await-timeout.svg" alt="License" /></a> | ||
</div> | ||
[![Build Status](https://travis-ci.org/vitalets/await-timeout.svg?branch=master)](https://travis-ci.org/vitalets/await-timeout) | ||
[![npm](https://img.shields.io/npm/v/await-timeout.svg)](https://www.npmjs.com/package/await-timeout) | ||
[![license](https://img.shields.io/npm/l/await-timeout.svg)](https://www.npmjs.com/package/await-timeout) | ||
A [Promise]-based API for setTimeout / clearTimeout. | ||
## Contents | ||
* [Installation](#installation) | ||
* [Usage](#usage) | ||
* [API](#api) | ||
* [new Timeout](#new-timeout) | ||
* [.set](#setms-message--promise) | ||
* [.clear](#clear) | ||
* [new Timeout()](#new-timeout) | ||
* [.set()](#setms-message--promise) | ||
* [.clear()](#clear) | ||
* [Motivation](#motivation) | ||
@@ -28,3 +29,4 @@ * [Related resources](#related-resources) | ||
## Usage | ||
Example of fetching resource with timeout (using ES7 [async / await]): | ||
The example below shows usage of timeout with [ES7 async / await] in `try...finally` block. | ||
It guarantees that timeout will be cleared in case of fetch success or any error: | ||
```js | ||
@@ -36,5 +38,5 @@ import Timeout from 'await-timeout'; | ||
try { | ||
const mainPromise = fetch('https://example.com'); | ||
const timerPromise = timeout.set(1000, 'Rejected by timeout'); | ||
await Promise.race([mainPromise, timerPromise]); | ||
const fetchPromise = fetch('https://example.com'); | ||
const timerPromise = timeout.set(1000, 'Timeout!'); | ||
const response = await Promise.race([fetchPromise, timerPromise]); | ||
} catch (e) { | ||
@@ -53,3 +55,3 @@ console.error(e); | ||
fetch('https://example.com'), | ||
timeout.set(1000, 'Rejected by timeout') | ||
timeout.set(1000, 'Timeout!') | ||
]) | ||
@@ -76,4 +78,5 @@ .then(result => { | ||
### .set(ms, [message]) ⇒ `Promise` | ||
Starts new timer like `setTimeout()` and returns promise. The promise will be resolved after `ms`: | ||
Starts new timer like `setTimeout()` and returns promise. The promise will be resolved after `ms` milliseconds: | ||
```js | ||
const timeout = new Timeout(); | ||
timeout.set(1000) | ||
@@ -95,6 +98,17 @@ .then(() => console.log('1000 ms passed.')); | ||
timeout.set(1000, 'Timeout'); | ||
// the same as | ||
// equivalent to | ||
timeout.set(1000).then(() => {throw new Error('Timeout')}); | ||
``` | ||
If you need to just wait some time: | ||
```js | ||
doAsyncJob() | ||
.then(() => new Timeout().set(1000)); | ||
// or there is static shortcut `Timeout.wait()` | ||
doAsyncJob() | ||
.then(() => Timeout.wait(1000)); | ||
``` | ||
### .clear() | ||
@@ -134,2 +148,3 @@ Clears existing timeout like `clearTimeout()`. | ||
* https://stackoverflow.com/questions/22707475/how-to-make-a-promise-from-settimeout | ||
* https://stackoverflow.com/questions/34255351/is-there-a-version-of-settimeout-that-returns-an-es6-promise | ||
@@ -141,2 +156,2 @@ ## License | ||
[Promise.race()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race | ||
[async / await]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | ||
[ES7 async / await]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
18672
10
250
151
3