1.0.5 • Published 4 years ago

@jsxtools/use-promise-factory v1.0.5

Weekly downloads
1
License
CC0-1.0
Repository
github
Last release
4 years ago

use-promise-factory

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);
}
1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago