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

@nolawnchairs/async

v1.0.1

Published

Utility wrappers for easier async programming in JavaScript

Downloads

4

Readme

Async Utils

The Async package contains functions that wrap the nastiness of setTimeout and allow cleaner code geared towards usage alongside async/await, as well as some abstractions that use timeouts and intervals to perform time-sensitive tasks.

Installation

npm i @nolawnchairs/async
yard add @nolawnchairs/async

Usage

Async.wait

async (ms: number) => Promise<void>

An asynchrounous function that waits for a given number of milliseconds ms and resolves. This is a convenience wrapper around setTimeout

async function foo() {
    console.log('Before')
    await Async.wait(1000)
    console.log('This prints one second later')
}

Async.delayed

async <T>(ms: number, callback: AsyncSupplier<T>) => Promise<T>

Delays execution of a function callback for a given amount of milliseconds ms, then returns the result of the callback.

function foo() {
    console.log('Before')
    Async.delayed(1000, () => console.log('This prints one second later'))
    const result = await Async.delayed(1000, () => 1 + 1)
    console.log(result) // 2
}

Async.scheduled

async (date: Date, callback: VoidFunction) => void

Schedules execution of a function callback to a specific date. This is a simple wrapper around setTimeout that does not save state, and should only be used on the backend, as using it on the front end will not re-schedule tasks after a page reload.

const date = new Date((Date.now() + 3600) * 1000)
Async.scheduled(date, () => console.log('This will be called in one hour'))

Async.waitUntil

async (condition: AsyncSupplier<boolean>, waitInterval?: number) => Promise<void>

For usage in an async function, this will delay progression of the async function until the boolean result of condition resolves to true. If the supplier never resolves, the function will not continue. Internally, this uses setInterval to check the truthiness of the supplier's outcome. The default interval frequency is 1 millisecond, but can be changed to a more lazy frequency by passing a millisecond value to waitInterval.

async function foo() {
    console.log('Before')
    await Async.waitUntil(() => resourcesAreLoaded())
    console.log('This will print when resources are loaded')
}

Async.waitUntilResolved

async (timeout: number, condition: AsyncSupplier<boolean>, waitInterval?: number) => Promise<void>

This does the same thing as Async.waitUntil, except that it takes a timeout in milliseconds as the first agument and will reject if the condition is not met before the timeout expires. Use inside of a try/catch block.

async function foo() {
    console.log('Before')
    try {
        await Async.waitUntilResolved(2000, () => resourcesAreLoaded())
        console.log('Resources loaded within 2 seconds')
    } catch {
        console.error('Resources were not loaded within 2 seconds')
    }
}

Async.awaitWithTimeout

async <T>(timeout: number, runner: AsyncSupplier<T>) => Promise<T>

For usage in an async function, this calls an async supplier function and will attempt to return its resolved value within the allotted timeout. If the function does not resolve before the timeout expires, an error is thrown. Use inside of a try/catch block.

try {
    const value = await Async.awaitWithTimeout<any>(10000, () => somePromiseThatMayNeverResolve())
    console.log('It resolved, we got the value!')
} catch {
    console.error('It never resolved, so timed out after 10000ms 💩')
}