Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-async-kit

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-async-kit

A TS kit to deal with async operations, loops, retries.. and so on!

  • 0.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

🚀 ts-async-kit

A lightweight TypeScript library that provides an easy-to-use API for dealing with common promise-related operations such as retrying and looping.

Installation

To install ts-async-kit in your project, simply run:

pnpm i ts-async-kit

Usage

Retry ↩️

Sometimes, a promise may fail due to a temporary issue such as a network error. In such cases, retrying the operation can be a useful strategy. With ts-async-kit, you can easily retry a promise until it succeeds or reaches a maximum number of attempts.

Here's an example:

import { retry } from 'ts-async-kit';

const fetchData = async () => {
  // Fetch data from an API
};

const result = await retry(fetchData, { maxRetries: 3 })

In the above example, fetchData is called up to three times, and the result is logged to the console if successful. If all attempts fail, an error is thrown.

ParameterDescription
MaxRetries (Optional)How many times the function will be executed
Interval (Optional)How long the function should wait to retry it again
Timeout (Optional)How long the function should wait until stops the function execution (and fail)
backoffFactor (Optional)How much the interval should increase after each retry
onRetry (Optional)Callback function that can be called with the error when a function is retried
onFail (Optional)Callback function that can be called with the error when the function execution finishes by MaxRetries OR Timeout

Sleep 😴

Sometimes, you just want to block the current flow of execution and wait a certain time. For you don't waste time searching on StackOverflow, we shipped a function that easily does that. It's called sleep.

Here's an example:

import { sleep } from 'ts-async-kit'

await sleep(1000) // Time in ms to sleep

Loop (WIP) 🏃‍♂️

Sometimes, you may need to perform a task repeatedly, such as polling an API for updates. With ts-async-kit, you can control how your loop will work. Error-tolerant, concurrent operations, until a condition is met, maximum number of iterations and so on...

Here's an example:


import { map } from 'ts-async-kit';

const data = [1, 2, 3]
const pollApi = async (id: number) => {
  // Poll an API for updates
};

const result = await map(data, pollApi, { concurrency: 2 })

In the above example, pollApi is called up until it finishes executing two promises concurrently. If all iterations fail, an error is thrown.

Contributing We welcome contributions! If you have an idea for a feature or would like to report a bug, please open an issue or submit a pull request.

License ts-async-kit is released under the MIT License.

Keywords

FAQs

Package last updated on 12 Jan 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc