@jsxtools/use-promise-factory
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

use-promise-factory

npm version build status issue tracker pull requests

use-promise-factory generates a hook that provides a state and settled value of a Promise.

It is 451 bytes (268 gzipped).

Installation

npm install @jsxtools/use-promise-factory

Usage

import { useEffect, useState } from 'react';
import usePromiseFactory from '@jsxtools/use-promise-factory';

const usePromise = usePromiseFactory({ useEffect, useState });

function Component () {
  // the `state` is "pending", "fulfilled", or "rejected"
  // the `settledValue` is the fulfilled or rejected value
  const [ state, settledValue ] = usePromise(async () => {
    const response = await fetch(URL);
    const json = await response.json();

    return json;
  });

  return state === 'pending'
    ? 'Loading'
  : JSON.stringify(settledValue);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @jsxtools/use-promise-factory

Weekly Downloads

4

Version

1.0.5

License

CC0-1.0

Unpacked Size

12.7 kB

Total Files

7

Last publish

Collaborators

  • jonathantneal