All Projects → inikulin → time-limit-promise

inikulin / time-limit-promise

Licence: MIT license
Fulfill long runinng promises on timeout.

Programming Languages

javascript
184084 projects - #8 most used programming language

time-limit-promise

Build Status

Fulfill long runinng promises on timeout.

Unlike other implementations on npm it has some nice extra features:

  • You can both reject and resolve promises on timeout
  • You can provide custom value with which promise will be rejected or resolved on timeout
  • Unrefs promise timer, so it will not block your app from exit.
  • Uses Promise implementation of the passed promise: no external implementation dependencies, no global Promise dependencies

Install

npm install time-limit-promise

Usage

const timeLimit = require('time-limit-promise');
const fetch     = require('node-fetch');

var fetchPromise = fetch('https://github.com/inikulin');

timeLimit(fetchPromise, 50).then(res => {
    // If `fetchPromise` will be fulfilled within 50ms
    // time limited promise will be fullfilled as well.
    // Otherwise, it will be resolved with the `undefined` value.
});

timeLimit(fetchPromise, 50, { resolveWith: 'no content' }).then(res => {
    // Same as above, but on timeout it will
    // be resolved with the `no-content` value.
    console.log(res); // > no-content
});


timeLimit(fetchPromise, 50, { rejectWith: new Error('timeout') }).catch(err => {
    // Same as above, but on timeout it will
    // be rejected with the provided error.
    console.log(err.message); // > timeout
});

Author

Ivan Nikulin ([email protected])

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].