🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

@nevware21/ts-async

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
t

@nevware21/ts-async

support for asynchronous development with a Promise based task Scheduler, several different Promise implementations (synchronous, idle, asynchronous and native runtime wrappers), await helpers, and aliases all built and tested using TypeScript.

0.5.4
latest
100

Supply Chain Security

100

Vulnerability

100

Quality

81

Maintenance

100

License

Version published
Weekly downloads
442K
0.83%
Maintainers
0
Weekly downloads
 
Created
Issues
1

What is @nevware21/ts-async?

@nevware21/ts-async is a TypeScript library that provides utilities for working with asynchronous operations. It includes features such as deferred promises, cancellation tokens, and utilities for working with async iterables.

What are @nevware21/ts-async's main functionalities?

Deferred Promises

Deferred promises allow you to create a promise and resolve or reject it at a later time. This is useful for scenarios where you need to control the timing of the promise resolution.

const { createDeferred } = require('@nevware21/ts-async');

const deferred = createDeferred();

setTimeout(() => {
  deferred.resolve('Hello, World!');
}, 1000);

deferred.promise.then((message) => {
  console.log(message); // Outputs: Hello, World!
});

Cancellation Tokens

Cancellation tokens provide a way to cancel asynchronous operations. This is useful for scenarios where you need to abort an operation based on certain conditions.

const { createCancellationTokenSource } = require('@nevware21/ts-async');

const cts = createCancellationTokenSource();

async function fetchData(token) {
  for (let i = 0; i < 5; i++) {
    if (token.isCancellationRequested) {
      console.log('Operation cancelled');
      return;
    }
    console.log('Fetching data...');
    await new Promise((resolve) => setTimeout(resolve, 1000));
  }
  console.log('Data fetched');
}

fetchData(cts.token);

setTimeout(() => {
  cts.cancel();
}, 2500);

Async Iterables

Async iterables allow you to work with asynchronous sequences of data. This is useful for scenarios where you need to process data that is received over time.

const { createAsyncIterable } = require('@nevware21/ts-async');

async function* asyncGenerator() {
  for (let i = 0; i < 5; i++) {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    yield i;
  }
}

const asyncIterable = createAsyncIterable(asyncGenerator);

(async () => {
  for await (const value of asyncIterable) {
    console.log(value); // Outputs: 0, 1, 2, 3, 4 (one per second)
  }
})();

Other packages similar to @nevware21/ts-async

FAQs

Package last updated on 15 Dec 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