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

async-chaining

v0.5.2

Published

The async-chaining library provides a toolkit for creating asynchronous chains of method and property calls. With its help, you can effectively manage asynchronous operations, increasing the efficiency and convenience of development.

Downloads

44

Readme

example workflow npm codecov

async-chaining

The async-chaining library provides a toolkit for creating asynchronous chains of method and property calls. With its help, you can effectively manage asynchronous operations, increasing the efficiency and convenience of development.

Live demo

Table of Contents

Why?

During the development process, we often encounter cases when we need to use a chain of asynchronous calls. For convenience, JavaScript provides us with additional tools in the form of promises and async/await constructs. About promise chaining

fetch('/article/promise-chaining/user.json')
  .then(response => response.json())
  .then(user => fetch(`https://api.github.com/users/${user.name}`))
  .then(response => response.json())
  .then(githubUser => console.log(githubUser.avatar_url));

The main disadvantage is that we have to write a lot of additional code. The library async-chaining offers a more convenient way to work with a chain of asynchronous calls.

async()
  .fetch('/article/promise-chaining/user.json')
  .json()
  .chain(user => fetch(`https://api.github.com/users/${user.name}`))
  .json()
  .avatar_url
  .then(console.log);

Getting Started

To install, run:

npm i async-chaining -S

Import async function and call it. You can also add asynchronous result properties and methods, prototypes, and array accessors to the chain. If there is an undefined property at any point in the chain or an error occurs, the entire result will return null.

import { async } from 'async-chaining';

async()
  .fetch('https://api.github.com/repositories')
  .json()
  .find(repo => repo.name === 'ambition')
  .id;

API

async(data, options): Proxy

The arguments are optional, but you can send a promise or some other data with the first argument, and options with the second argument

async(fetch('https://api.github.com/repositories'), { 
  debug: true, // default false - see more details .debug()
  strict: true // default false - throws an error if the method or property does not exist
})
  .fetch('https://api.github.com/repositories')
  .json()

.chain(fun): Promise<fun.apply(Transfered data)>

You can also use a special .chain method to combine chains by passing a function to the method. The function will take as arguments the result of the execution of the previous promise.

async()
  .fetch('https://api.github.com/repositories')
  .json()
  .find(repo => repo.name === 'ambition')
  .id
  .chain((id) => fetch('https://api.github.com/repositories/' + id))
  .json()
  .watchers;

.progress(fun): Transfered data

The .progress method accepts a function to which the data from the previous step is passed, but the .progress method does not return the data, unlike the .chain method. Can be used to update the progress of the execution of the chain.

async()
  .progress(() => console.log('start'))
  .fetch('https://api.github.com/repositories')
  .progress(() => console.log('in progress'))
  .json()
  .progress(() => console.log('end'));

.debug(): Transfered data

The .debug method can help you to debug the chain. Its call activates debugging mode and each step of the chain will be logged into the console.

async()
  .debug()
  .fetch('https://api.github.com/repositories/26')
  .json()
  .watchers;

// trap get: add microtask, target: [object Window], property: fetch
// trap apply: add microtask, data: [object Window],function fetch() { [native code] }
// trap get: add microtask, target: [object Response], property: json
// trap apply: add microtask, data: [object Response],function json() { [native code] }
// trap get: add microtask, target: [object Object], property: watchers

Examples

await

(async () => {
  const repoId = await async(fetch('https://api.github.com/repositories'))
    .json()
    .find(repo => repo.name === 'ambition')
    .id;
    
  console.log(repoId);
})();

.then .catch .finally

async()
  .fetch('https://api.github.com/repositories')
  .json()
  .then(console.log)
  .catch((e) => console.err(e))
  .finally(() => console.log('finally'))

axios

import axios from 'axios';

const a = async(axios.get('https://api.github.com/repositories'))
  .data
  .find(repo => repo.name === 'ambition')
  .id
  .then(console.log);

Pros

There are some other packages for a similar use case, but this one is:

  • Tiny: ~20KB minified.
  • Well tested: 100% test coverage.
  • Safe: No known vulnerabilities according to npm audit.
  • Self contained: No external dependencies (only devDependencies).

Limitations

The library supports use in the browser and is implemented using a built-in Proxy object that allows you to intercept calls in the chain. Browser Compatibility