1. time-limit-promise
Fulfill long runinng promises on timeout.
time-limit-promise
Package: time-limit-promise
Created by: inikulin
Last modified: Mon, 27 Jun 2022 06:43:58 GMT
Version: 1.0.4
License: MIT
Downloads: 934,844
Repository: https://github.com/inikulin/time-limit-promise

Install

npm install time-limit-promise
yarn add time-limit-promise

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])

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code