promised-timeout
v0.5.1
Published
For limiting the time to resolve a promise.
Downloads
14,730
Maintainers
Readme
promised-timeout
For limiting the time to resolve a promise.
This is a light weight open source package, written with TypeScript. The source code is available on GitHub where you can also find our issue tracker.
Related Projects
- promised-debounce: For debounced function execution.
Motivation
Using the native Promise.race
method for implementing the Promise.timeout
function is not sufficient. The Promise.race
doesn't clear the timer of the timeout promise after the actual promise resolves thus the process will wait until the timeout promise is also complete. This means that if you set the timeout to 1h and our promise completes after 1min, the process will wait for another 59min before it exits.
Install
$ npm install --save promised-timeout
Example
import { timeout } from 'promised-timeout';
await timeout({
action: () => new Promise(), // your promise here
time: 100,
error: new Error('operation timeout')
});
API
timeout({ action, timeout, error }):Promise
A timeout helper function resolves the provider promise but rejects if the operation takes too long.
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
| action | Function,Promise | Yes | - | A Promise object to resolve.
| time | Integer | No | 0 | A time in milliseconds after the operation automatically rejects (0
disables the timeout).
| error | Error | No | new Error() | A custom error object to pass to the reject
handler.
License (MIT)
Copyright (c) 2016 Kristijan Sedlak <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.