You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@nevware21/ts-async

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@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
Source
npmnpm
Version published
Weekly downloads
503K
6.02%
Maintainers
2
Weekly downloads
 
Created

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

Keywords

typescript

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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.