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

use-async-machine

v0.1.7

Published

A hook for an async state machine

Downloads

2

Readme

use-async-machine

A hook for using an async state machine. The state machine this library implements looks like this (image generated with XState Visualizer):

Usage

This library exports a single hook to help you with asynchronous operations in your UI.

import React from 'react'
import useAsync from 'use-async-machine'

function someAsyncTask() {
  // Returns a promise
}

export default function App() {
  const { data, error, isLoading, isError, dispatch } = useAsync(() => someAsyncTask())

  React.useEffect(() => {
    dispatch({ type: 'load' })
  }, []) // 'dispatch' does not need to be included in dependency array

  if (isLoading) {
    return <Spinner />
  }

  if (isError) {
    return <ErrorMessage error={error}>
  }

  return <div>Here's the result: {data}</div>
}

Use Cases

This library was created out of a need to generate and render a PDF document in the browser. useAsync is most useful for client-only asynchronous tasks such as this. If you need to interact with remote data sources, I recommend you try a data caching library such as react-query or use an SSR framework such as Remix.

API Reference

const {
  state,
  data,
  error,
  context,
  isIdle,
  isLoading,
  isError,
  isSuccess,
  dispatch,
} = useAsync(fn, {
  onLoad,
  onSuccess,
  onError,
});

Options

  • fn: (state: UseAsyncState<TData, TError, TContext>) => Promise<TData>
    • The asynchronous function to run on the load event.
    • Receives the async state.
    • Must return a promise that either resolves to data or throws an error.
  • onLoad: (state: UseAsyncState<TData, TError, TContext>) => unknown
    • Function to be called when the state machine enters the loading state.
    • Receives the async state
    • Returns nothing
  • onSuccess: (state: UseAsyncState<TData, TError, TContext>) => unknown
    • Function to be called when the state machine enters the success state.
    • Receives the async state
    • Returns nothing
  • onError: (state: UseAsyncState<TData, TError, TContext>) => unknown
    • Function to be called when the state machine enters the error state.
    • Receives the async state
    • Returns nothing

Returns

  • state: "idle" | "loading" | "success" | "error"
    • Defaults to "idle"
    • The state of the async function.
  • data: TData
    • Defaults to undefined
    • The resolved data returned by fn
  • error: TError
    • Defaults to undefined
    • If fn rejects with an error, it will be stored here
  • context: TContext
    • Defaults to undefined
    • May be set by the "load" event with dispatch({ "type": "load", context })
    • Useful if you want to pass additional context to fn
  • isIdle: boolean
    • Derived from state, true if and only if state === "idle"
  • isLoading: boolean
    • Derived from state, true if and only if state === "loading"
  • isError: boolean
    • Derived from state, true if and only if state === "error"
  • isSuccess: boolean
    • Derived from state, true if and only if state === "success"
  • dispatch: React.Dispatch<UseAsyncEvent<TData, TError, TContext>>
    • Dispatch function returned from React.useReducer
    • Used to transition between async states.