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

taskmap

v1.0.0

Published

Optimally define and execute sets of dependent tasks

Downloads

19

Readme

taskmap

Declare a set of tasks and their dependencies, then run them in an optimized manner. Tasks all run in parallel unless they have to wait on a dependency, and if a task is a dependency for multiple tasks it's only run once.

Usage

import { orchestrate } from "taskmap";

async function example() {
  const { sum, mean } = await orchestrate
    .task("data", () => fetchNumbers())
    .task("sum", ["data"], ({ data }) => data.reduce((sum, n) => sum + n, 0))
    .task("count", ["data"], ({ data }) => data.length)
    .task("mean", ["sum", "count"], ({ sum, count }) => sum / count);
}

API

orchestrate

The orchestrate object gives you the starting point for creating an Orchestration.

import { orchestrate } from "taskmap";

Orchestration

An Orchestration is an object that lets you define tasks and execute them. It has the following methods:

.task(id, fn)

.task(id, dependencies, fn)

The task method defines a new task, returning a new Orchestration containing previously-defined tasks along with the new one. You must supply an id for your task and a function fn. The function may or may not return a value, and it may be either synchronous or asynchronous.

If your task function depends on any of the previously-defined task results, you can add a dependencies Array containing one or more task IDs. An object map with those same task IDs and the results of those tasks will be passed to your function, with all Promises having been resolved.

The task's function will not be executed until the Orchestration is awaited/resolved.

.promise()

Finalizes the Orchestration, returning a Promise that resolves to an map object with every task's ID as a key and each value being the resolved result of the task function.

.then(...)

As a shortcut, an Orchestration is a also a "thenable" so you can directly await it without having to call .promise(). Note that if you're planning on awaiting an Orchestration multiple times, it does not behave like a Promise because each time it's awaited it re-runs the tasks. Use .promise() if you want a real Promise.

.resolve(...ids)

If you want to execute a subset of tasks, the .resolve() function lets you pass the ID of each task you want to run, and the resolved result only contains the key/values of those tasks. Tasks that were not specified will not be run unless they are dependencies of the specified tasks.

Inspiration

This package is heavily inspired by the auto function of async. The reason for creating a new package is that async.auto doesn't work well with TypeScript inference due to language limitations. While the API isn't quite as terse as async.auto, it does let you define your tasks in a way that enables TypeScript to infer the types of every task and dependency.

Language compatibility

  • JavaScript - taskman is compiled to ES2021 and is a CommonJS library, but if you find it useful and need compatibility with other environments, we will consider accommodating. It has no dependencies.
  • TypeScript - taskman is written in TypeScript 4.6.3.