npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

t-tasks

v1.1.1

Published

T-Tasks provides a Task quasi-monad, allowing to schedule, cancel and externally reject async operations in a composable, semi-functional way.

Downloads

239

Readme

CI

T-Tasks task library

T-Tasks provides a Task quasi-monad, allowing to schedule, cancel and externally reject async operations in a composable, semi-functional way.

Sometimes (for example, within React hooks) you need to cancel any ongoing asynchronous operation to prevent side-effects. Switching from async functions to chained tasks one can simply cancel current task execution ensuring that the operation will be cancelled at the nearest spot, preventing any unwanted side efects afterwards.

Examples

Prevent side-effects after async function

Keep in mind, that pure promises are still not cancelable, so the original function will still finish. In this scenario we can only elliminate side effects within task composition chain.

const task = Task.fromPromise(someAsyncFunction())
  .tap((result) => {
    setSomething(result); // side-effects
  });

task.cancel(); // prevent unwanted side effects

Chain multiple async functions togeter

In this scenario if one cancels the task during first operation, the seconf one would not be initiated at all

const task = Task.fromPromise(someAsyncFunction())
  .chain((result1) => otherAsyncFunction(result1))
  .tap((result2) => {
    setSomething(result); // side-effects
  });

task.cancel(); // prevent unwanted side effects

Chain multiple async functions via generator

In this scenario is identical to the previous one except of using generator syntax. Note usage of .generator() is required due to typescript being unable to predict types correctly othewise.

const task = Task.generate(function*() {
  const result1 = yield* Task.fromPromise(someAsyncFunction()).generator();

  const result2 = yield* Task.fromPromise(otherAsyncFunction(result1)).generator();
  
  setSomething(result2); // side-effects
});

task.cancel(); // prevent unwanted side effects

Throw exceptions straight to generators

It is also possible to reject pending tasks manually, resulting in an immediate interruption with exception. However you still can catch that exception within the task itself and fallback if necessary.

const task = Task.generate(function*() {
  let result1: number;

  try {
    result1 = yield* Task.promiseGenerator(someAsyncFunction()); // equal to Task.fromPromise(<...>).generator();
  } catch (e) {
    console.error(e);

    result1 = 42; // Sometimes we have to give an answer in time i guess
  }

  const result2 = yield* Task.promiseGenerator(otherAsyncFunction(result1)); // equal to Task.fromPromise(<...>).generator();
  
  setSomething(result2); // side-effects
});

task.reject(new Error('Stop right there')); // prevent unwanted side effects