before-timeout

1.0.0 • Public • Published

Rejects a promise that takes too much time

Build Status

Install

$ npm install before-timeout

Usage

const beforeTimeout = require('before-timeout');
 
// Print "good boy! 🐶"
beforeTimeout(Promise.resolve('good boy! 🐶'), 1000)
  .then(res => console.log(res))
  .catch(() => console.log('2 slow 🐾'))
 
// prints "2 slow 🐾""
const sleep = require('sleep-promise')
beforeTimeout(sleep(2000), 1000)
  .then(res => console.log('🐶'))
  .catch(() => console.log('2 slow 🐾'))
 
// async/await
try {
  const res = await beforeTimeout(Promise.resolve('good boy! 🐶'), 1000)
  console.log(res)
} catch (e) {
  console.log('2 slow 🐾')
}
 

API

beforeTimeout(promise, timeout)

Creates a race between the promise and the timeout. If the promise takes longer than the time the promise will be rejected.

License

MIT © Max Malm

Readme

Keywords

Package Sidebar

Install

npm i before-timeout

Weekly Downloads

2

Version

1.0.0

License

MIT

Last publish

Collaborators

  • benjick