New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

timeout-decorator

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

timeout-decorator

A decorator that enforces a time limit on the execution of an async method.

latest
Source
npmnpm
Version
1.0.3
Version published
Maintainers
1
Created
Source

timeout-decorator

A TypeScript decorator to enforce a time limit on the execution of async methods.

Step 1: Installation

You can install the package via npm:

npm install timeout-decorator

Step 2: Update tsconfig.json

After installing the package, you need to enable the experimentalDecorators option in your tsconfig.json file to use decorators in TypeScript.

Add the following line to your tsconfig.json:

{
    "compilerOptions": {
        "experimentalDecorators": true
    }
}

Usage Example

Consider a scenario where you have an async method fetchData within a class DataFetcher, which fetches data from an external API using await. You want to ensure any errors during this asynchronous operation are handled gracefully.

import { Timeout } from 'timeout-decorator';

class DataFetcher {
    @Timeout(5000) // 5 seconds timeout
    async fetchData() {
        // Fetch data from an API
        const response = await fetch('https://api.example.com/data');
        const data = await response.json();
        // Process the data
        return data;
    }
}

In this example, the Timeout decorator ensures that the fetchData method completes within 5 seconds. If the method takes longer than the specified timeout duration, it will be rejected with a timeout error

Additional Notes

  • The Timeout decorator should be applied to async methods.
  • Specify the timeout duration in milliseconds as the parameter to the Timeout decorator.
  • If the method completes within the specified timeout duration, its result is returned as usual.
  • If the method exceeds the timeout duration, it will be rejected with a timeout error. You can handle this error using a .catch() block when calling the method.

Keywords

timeout

FAQs

Package last updated on 10 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts