Get unlimited public & private packages + team-based management with npm Teams.Learn more »

simple-timeout-promise

1.0.6 • Public • Published

Simple TimeoutPromise

This javascript module exports TimeoutPromise and TimeoutError to allow an ease-of-use usage of promises that reject after a given timeout, unless their purpose was either resolved or rejected before the given timeout.

Motivation

I was implementing this for work, and couldn't find any good examples online, so I figured I'd publish a module. The timeout promise modules that I did find, made usage of Promise.race() which isn't supported by Internet Explorer, This module uses ES6 only and supports all browsers.

Installation

Simply install this module in your project with npm

$ npm install simple-timeout-promise --save

Usage

Given a function that returns a promise - promiseFunc, and a delay in miliseconds, create a wrapped promise:

const { TimeoutPromise, TimeoutError } = require('simple-timeout-promise')
 
const timeoutPromise = new TimeoutPromise(delay, promiseFunc)
timeoutPromise.then(result => {
  // Enter your resolve logic here
}).catch(error => {
  if (error instanceof TimeoutError) {
    // Enter your timeout rejection logic here
  }
  // Otherwise, enter your other rejections logic here
})

Contribution

Please see CONTRIBUTING.md

Install

npm i simple-timeout-promise

DownloadsWeekly Downloads

1

Version

1.0.6

License

MIT

Unpacked Size

11.9 kB

Total Files

9

Last publish

Collaborators

  • avatar