suspend-concurrently
TypeScript icon, indicating that this package has built-in type declarations

0.5.0 • Public • Published

Suspend Concurrently

Promise concurrency primitives for React Suspense.

  • suspendAll(promises)
  • suspendRace(promises)
  • suspendAny(promises)
  • suspendAllSettled(promises)

These work just like the Promise.all, Promise.race, Promise.any, and Promise.allSettled methods, but are suitable to be used inside of React 18. See the MDN docs for more information.

Usage

These utilities are intended to be used with Remix's deferred data APIs, but they can also be used anywhere you need to compose multiple promises together and resolve them concurrently.

In remix:

export function loader() {
  return defer({
    deferredUserName: fetchUserName(),
    deferredUserAvatar: fetchUserAvatar(),
  });
}

export default function UserRoute() {
  const {deferredUserName, deferredUserAvatar} = useLoaderData();
  const userPromise = suspendAll([deferredUserName, deferredUserAvatar]);

  return (
    <Suspense fallback={<Loading />}>
      <Await resolve={userPromise}>
        {([name, avatar]) => (
          <div>
            <img src={avatar} />
            <h1>{name}</h1>
          </div>
        )}
      </Await>
    </Suspense>
  );
}

Package Sidebar

Install

npm i suspend-concurrently

Weekly Downloads

0

Version

0.5.0

License

MIT

Unpacked Size

27.2 kB

Total Files

12

Last publish

Collaborators

  • tom.sherman