Skip to content

Latest commit

 

History

History
62 lines (43 loc) · 1.29 KB

README.md

File metadata and controls

62 lines (43 loc) · 1.29 KB

timedout-promise-error

An easy way to put a timeout on a promise with a custom error


Installation

yarn add timedout-promise-error
npm i timedout-promise-error --save

Usage

import timedoutPromiseError from "timedout-promise-error"

// simple GET request:
try {
    const response = await timedoutPromiseError(fetch('https://www.somewhere.com'), 5000, {
      status: 504,  url: 'https://www.somewhere.com'
    });
} catch (error) {
    console.log(error.url); //https://www.somewhere.com
    console.log(error.status); //504
    console.log(error.stack); // Error.stack
}

API

timedout-promise-error allows you to put a timeout on a promise while providing a custom error for the timeout. Your error is returned in a Error so you get the error stack too. If the passed in promise has an error, it functions as expected

timedoutPromiseError(somePromise: Object, timeout: Number, error: Object)

// simple GET request:
try {
    const response = await timedoutPromiseError(fetch('https://www.somewhere.com'), 5000, {
      status: 504, url: 'https://www.somewhere.com'
    });
} catch (error) {
    console.log(error.url); //https://www.somewhere.com
    console.log(error.status); //504
    console.log(error.stack); // Error.stack
}