await-timeout
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -1,2 +0,2 @@ | ||
/*! await-timeout v0.1.5 */ | ||
/*! await-timeout v0.1.6 */ | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
@@ -118,2 +118,15 @@ if(typeof exports === 'object' && typeof module === 'object') | ||
}, { | ||
key: 'wrap', | ||
value: function wrap(promise, ms) { | ||
var _this2 = this; | ||
var msg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; | ||
var wrappedPromise = promiseFinally(promise, function () { | ||
return _this2.clear(); | ||
}); | ||
var timer = this.set(ms, msg); | ||
return Promise.race([wrappedPromise, timer]); | ||
} | ||
}, { | ||
key: 'clear', | ||
@@ -128,5 +141,2 @@ value: function clear() { | ||
// Static `.set()` helper | ||
Timeout.set = function (ms, msg) { | ||
@@ -136,2 +146,18 @@ return new Timeout().set(ms, msg); | ||
Timeout.wrap = function (promise, ms, msg) { | ||
return new Timeout().wrap(promise, ms, msg); | ||
}; | ||
function promiseFinally(promise, fn) { | ||
var success = function success(result) { | ||
fn(); | ||
return result; | ||
}; | ||
var error = function error(e) { | ||
fn(); | ||
return Promise.reject(e); | ||
}; | ||
return Promise.resolve(promise).then(success, error); | ||
} | ||
exports.default = Timeout; | ||
@@ -138,0 +164,0 @@ module.exports = exports['default']; |
{ | ||
"name": "await-timeout", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "A Promise-based API for setTimeout / clearTimeout", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -18,2 +18,3 @@ <div align="center"> | ||
* [.set()](#setms-message--promise) | ||
* [.wrap()](#wrap-promise-ms-message--promise) | ||
* [.clear()](#clear) | ||
@@ -30,4 +31,4 @@ * [Motivation](#motivation) | ||
## Usage | ||
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: | ||
The example below shows how to set timeout for fetching `example.com` using [ES7 async / await] syntax. | ||
The code is wrapped into `try...finally` block that guarantees the timeout will be properly cleared: | ||
```js | ||
@@ -41,3 +42,3 @@ import Timeout from 'await-timeout'; | ||
const timerPromise = timeout.set(1000, 'Timeout!'); | ||
const response = await Promise.race([fetchPromise, timerPromise]); | ||
return await Promise.race([fetchPromise, timerPromise]); | ||
} catch (e) { | ||
@@ -50,3 +51,3 @@ console.error(e); | ||
``` | ||
The same example using `.then`: | ||
The same example with `.then`: | ||
```js | ||
@@ -70,2 +71,10 @@ function foo() { | ||
The same example using [Timeout.wrap()](#wrap-promise-ms-message--promise) has less code: | ||
```js | ||
function foo() { | ||
const promise = fetch('https://example.com'); | ||
return Timeout.wrap(promise, 1000, 'Timeout!').catch(e => console.error(e)); | ||
} | ||
``` | ||
## API | ||
@@ -110,2 +119,9 @@ ### new Timeout() | ||
### .wrap(promise, ms, [message]) ⇒ `Promise` | ||
Wraps promise into timeout that automatically cleared if promise gets fulfilled. | ||
```js | ||
Timeout.wrap(fetch('https://example.com'), 1000, 'Timeout!') | ||
.catch(e => console.error(e)); | ||
``` | ||
### .clear() | ||
@@ -112,0 +128,0 @@ Clears existing timeout like `clearTimeout()`. |
@@ -17,2 +17,8 @@ /** | ||
wrap(promise, ms, msg = '') { | ||
const wrappedPromise = promiseFinally(promise, () => this.clear()); | ||
const timer = this.set(ms, msg); | ||
return Promise.race([wrappedPromise, timer]); | ||
} | ||
clear() { | ||
@@ -23,5 +29,22 @@ clearTimeout(this._id); | ||
// Static `.set()` helper | ||
Timeout.set = (ms, msg) => new Timeout().set(ms, msg); | ||
Timeout.set = function (ms, msg) { | ||
return new Timeout().set(ms, msg); | ||
}; | ||
Timeout.wrap = function (promise, ms, msg) { | ||
return new Timeout().wrap(promise, ms, msg); | ||
}; | ||
function promiseFinally(promise, fn) { | ||
const success = result => { | ||
fn(); | ||
return result; | ||
}; | ||
const error = e => { | ||
fn(); | ||
return Promise.reject(e); | ||
}; | ||
return Promise.resolve(promise).then(success, error); | ||
} | ||
export default Timeout; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23920
11
343
165
0