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-lube

v1.6.0

Published

Simplify Asynchronous Operations in JavaScript

Downloads

17

Readme

Async Lube

Simplify Asynchronous Operations in JavaScript

Managing asynchronous operations in JavaScript can often be complex and error-prone, especially when dealing with dependencies and handling handlers.
However, with the "async-lube" library, working with asynchronous code becomes a seamless experience.
This powerful JavaScript library provides a set of functions that simplify asynchronous operations and empower developers to write clean and efficient code.

installation

npm i async-lube
pnpm i async-lube
bun i async-lube

How to use

Ergonomic fetch api

import { client } from "async-lube"

client("https://...", abort => setTimeout(abort, 2000))
    .get({ cache: "default" })
    .query({ id: 1 }, { Authorization: "..." })
    .then(res => res.json())

client("https://...")
    .post({ cache: "default" })
    .json({ string: "abc", number: 123 })
    .then(res => res.json())

Ensure execution order of complex asynchronous flows with DAG

import { dag } from "async-lube"

async function func_a(): string {
    return ""
}
function func_b(a: string): number {
    return +a
}
let count = 0
async function func_c(a: string, b: number) {
    return ++count + a + b
}

var async_flow = dag()
    .add(func_a)
    .add(func_b, func_a)
    .add(func_c, func_a, func_b)

await async_flow() //=> "10"
await async_flow() //=> "20"
await async_flow() //=> "30"

var type_error_flow = dag()
    .add(func_a, "") // Expected 1 arguments, but got 2.ts(2554)
    .add(func_b) // Expected 2 arguments, but got 1.ts(2554)
    .add(func_b, 1) // Argument of type 'number' is not assignable to parameter of type ...ts(2345)

Simplifies complex asynchronous functionality with Decorator

import { decorator } from "async-lube"

var api = decorator((id: string) => fetch("https://.../" + id))
    .cache(10) // s
    .debounce(200) // ms
    .retries((reason, count) => {
        if (count > 3) {
            throw reason // throwing an error does not retry
        }
    })
    .throttle(3, 500) // n request in ms

api("123") // caching key: "[\"123\"]"

api(123) // Argument of type 'number' is not assignable to parameter of type 'string'.ts(2345)
api() // Expected 1 arguments, but got 0.ts(2554)

Parallel runs that limit the maximum number of concurrent runs

import { parallel } from "async-lube"

parallel(2, () => ({ a: "a", b: "b" }), () => 123, () => {})
    .then(result => {
        if ("value" in result[0]) {
            result[0].value.a
        } else {
            throw result.reason
        }
        if ("value" in result[1]) {
            result[1].value.toFixed()
        } else {
            throw result.reason
        }
        if ("value" in result[2]) {
            result[2].value[""] // Property '' does not exist on type 'void'.ts(7053)
        }
    })