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

brief-async

v0.5.0

Published

The most brief syntax for describing async flow

Downloads

12

Readme

Brief-async

The shortest syntax for defining async flow. It helps to handle difficult cases with many parallel async operations.

Installation

This lib is experimental. Its API may change soon

npm i -S brief-async

Example

  1. At the start we have a value 'foo' and want to immediately start 3 functions parallel.
  2. Then, after both fn1 and fn2 are finished, their results become an argument for fn4.
  3. Then, after both fn3 and fn4 are finished, a finish function will start using their results.
// fn1-fn4 and finish functions are declared somethere above.
const flow = require('brief-async');
const steps = [
  ['foo'],      fn1, fn2, fn3,
  [fn1, fn2],   fn4,
  [fn3, fn4],   finish,
];
const result = await flow(steps);

Explanation

The steps is just an array. It contains functions and their dependencies.

  1. The first element is an array of dependencies which will be resolved and then taken by the second element.
  2. The second element must be a function or it will be ignored instead.
  3. The third element may be a function or an array.
const steps = [
  // 'foo' is the one argument for fn1-fn3 which work parallel
  ['foo', 123],       fn1, fn2, fn3, deadEnd,
  // f1 and fn2 after resolving pack their results to an array which is the first argument for fn4
  [fn1, fn2, fnArg],  fn4,
  // results of f3 and fn4 become go to finish as an array
  [fn3, fn4],         finish,
];
// result is a Promise
// errHandler is a callback to execute when a reject function is called
// if isMilestones == true, intermediate results will be included to the final result
const result = flow(steps, errHandler, isMilestones);

Step handlers or executors are functions with 3 arguments:

  1. Data from the previous step (array or a single value).
  2. Resolve callback.
  3. Reject callback.

2-nd and 3-rd arguments is needed to finish a Promise. An executor function must call one of those callbacks instead of using return statement. The first argument is polymorphic: a value or an array; it depends on the count of dependencies.

Important: function deadEnd does not have resolve function as an argument. But it has a reject function. So, "dead end" functions may break the whole chain, but they can not resolve the chain.