0.0.3 • Published 10 years ago

timeout-after v0.0.3

Weekly downloads
5
License
MIT
Repository
github
Last release
10 years ago

Add a timeout to an existing function.

timeout-after uses the global Promise so you might have to polyfill for node.

Installation

npm install timeout-after

Usage

Create a new function from an existing one:

import timeoutAfter from 'timeout-after';

const fetchWithTimeout = timeoutAfter(5000, fetch);

// The new function will return a promise, which gets rejected if it takes too
// long.
(async function() {
  try {
    await fetchWithTimeout(...);
  } catch (err) {
    if (err.name === 'Timeout') console.error('Uh oh! Timed out!');
    throw err;
  }
}());

It curries too:

import timeoutAfter from 'timeout-after';

const withTimeout = timeoutAfter(5000);
const fetchWithTimeout = withTimeout(fetch);