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

fun-task

v1.5.2

Published

An abstraction for managing asynchronous code in JS

Downloads

2,062

Readme

fun-task* Build Status Coverage Status

An abstraction for managing asynchronous code in JS.

* The name is an abbreviation for "functional task" (this library is based on many ideas from Functional Programming). The type that library implements is usually referred as just Task in docs.

Installation

NPM

npm install fun-task
// modern JavaScript
import Task from 'fun-task'

// classic JavaScript
var Task = require('fun-task')

CDN

<script src="https://unpkg.com/fun-task/umd/funTask.js"></script>
<script>
  var Task = window.FunTask
</script>

What is a Task?

Task is an abstraction similar to Promises. The key difference from Promises is that a Task represents a computation while a Promise represents only the result of a computation. Therefore if we have a Task we can: start the computation, terminate it before it finished, or wait until it finishes and get the result. While with a Promise we can only get the result. This difference don't make Tasks better, they are just different from Promises and we can find legitimate use cases for both abstractions. Let's review it again:

If we have a Task:

  • We can start the computation that it represents (e.g. a network request)
  • We can choose not to start the computation and just throw task away
  • We can start it more than once
  • While computation is running we can notify it that we don't interested in the result any more, and as a response computation can choose to terminate itself
  • When computation finishes we get the result

If we have a Promise:

  • Computation is already running (or finished) and we don't have any control of it
  • We can get the result whenever it's ready
  • If two or more consumers have a same Promise they all will get the same result

The last item is important. This is the key advantage of Promises over Tasks. Tasks don't have this feature. If two consumers have a same Task, each of them have to spawn their own instance of the computation in order to get the result, and they may even get different results.

What is a computation?

function computation(onSuccess, onFailure) {
  // ...
  return () => {
    // ... cancellation logic
  }
}

From Task API perspective computation is just a function that accepts two callbacks. It should call one of them after completion with the final result. Also a computation may return a function with cancellation logic or it can return undefined if particular computation has no cancellation logic.

Creating a Task from a computation is easy, we just call task = Task.create(computation). This is very similar to new Promise(computation), but Task won't call computation immediately, the computation starts only when task.run() is called (unlike with Promises where computation is started immediately).

Documentation

Flow

The NPM package ships with Flow definitions. So you can do something like this if you use Flow:

// @flow

import Task from 'fun-task'

function incrementTask<F>(task: Task<number, F>): Task<number, F> {
  return task.map(x => x + 1)
}

Specifications compatibility

Task is compatible with Fantasy Land and Static Land implementing:

Development

npm run lobot -- --help

Run lobot commands as npm run lobot -- args...