Skip to content

timbielawski/timedout-promise-error

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published