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-simple-async

v1.4.2

Published

The simplest(and lightest with *less than 500 bytes gzipped*) way to execute an asynchronous function in react

Downloads

23

Readme

useSimpleAsync

The simplest(and lightest with less than 500 bytes gzipped) way to execute an asynchronous function in react

NOTE: This library is not supposed to be an alternative for useSWR or react-query. It is simply a lighter version of them, if you don't need all the features that those libraries provide

Ever wanted to simply execute an async function without all the hassle and you don't want cache or anything like that? This is it. Supplies both CJS and ESM builds.

Installation

npm install use-simple-async

or

yarn add use-simple-async

Usage

import useSimpleAsync from "use-simple-async";
import fs from "filesystem";

const getIOData = async (path: string) => await fs.readDir(path); // [{name: string, path: string}]
const fetchComplexData = (arg1: string, arg: { internalArg: string }) => string;

const App = () => {
  // with one argument
  const [data, { loading, error }] = useSimpleAsync(getIOData, {
    variables: "/",
  });
  // or - with multiple arguments - all of this is typesafe!
  const [data, { loading, error }] = useSimpleAsync(fetchComplexData, {
    variables: ["asd", { internalArg: "asd" }],
  });
  // ---

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Something went wrong.</div>;

  return <div>{data.map((e) => e.name)}</div>;
};

API

function useSimpleAsync<T, V extends Array<any>>(
  asyncFunc: (...variables: V) => Promise<T>,
  options: { skip?: boolean; variables: V, useLayout?: boolean }
): [T | undefined, FuncMeta];

function useSimpleAync<T, V>(
  functionToExec: (variables?: V) => Promise<T>,
  options?: { variables?: V; skip?: boolean, useLayout?: boolean }
): [T, { loading: string; error: unknown; retry: () => void }];

function useSimpleAsync<T, V>(
  asyncFunc: (variables: V) => Promise<T>,
  options: { skip?: boolean; variables: V, useLayout?: boolean }
): [T | undefined, FuncMeta];

function useSimpleAsync<T>(
  asyncFunc: (variables?: undefined) => Promise<T>,
  options?: { skip?: boolean; variables?: undefined, useLayout?: boolean }
): [T | undefined, FuncMeta];

useLayout: true will execute your async function in useLayoutEffect instead of useEffect(default)

Refetching

Hook will automatically refetch when you change the function reference you provide to useSimpleAsync. A simple recipe for e.g. fetching data with different variables would look like this:

import useSimpleAsync from "use-simple-async";

const fetchSimpleData = (arg1: string) => string;

const App = () => {
  const [variables, setVariables] = useState({ input: "hello" });

  // Option one(recommended): Let the hook handle variables
  const [data, { loading, error }] = useSimpleAsync(fetchSimpleData, {
    variables,
  });
  // ---

  // Option two: Handle everything yourself
  // useCallback is important here!
  const fetchData = useCallback(
    () => fetchSimpleData(var1, var2, var3),
    [var1, var2, var3]
  );
  const [data, { loading, error }] = useSimpleAsync(fetchData);
  // ---

  const handleChangeVariables = () => {
    setVariables({ input: "world!" });
  };

  if (loading) return "Loading...";

  return (
    <div>
      {data?.output}
      <button onClick={handleChangeVariables}>change variables</button>
    </div>
  );
};

Submitting errors

If you see that something is not working for you or you'd like it to work differently, please don't hesistate to open a issue!

Why not use XXX?