New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

useawait

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

useawait

use promise and async function with suspense api

latest
Source
npmnpm
Version
0.0.4
Version published
Maintainers
1
Created
Source

useAwait

works with react suspense.

React controls the behavior of suspense based on the throw statement. Therefore, the behavior of useAwait in the component behaves like the await function inside the async function.

It is very simple to use.

It only takes 5 minutes to read the code below. For now, that's all.

  • Write your own asynchronous function that returns a promise.
  • It can be a function or a list of functions. If you need multiple asynchronous processing at any given time, create a list. It runs in parallel. This will be the first parameter.
  • If there is a dependency on the asynchronous processing you just wrote, pass it as a list to the second parameter. Maybe it will be something like a user ID.
function UseAwaitTest1() {
  const [count, setCount] = useState(0);

  const getUserName = (count) => () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          name: "Lee Seokkyu",
          count,
        });
      }, 5000);
    });
  };

  const result = useAwait(getUserName(count), [count]);

  return (
    <div>
      <div>count : {count}</div>
      <div>name : {result ? result.name : ""}</div>
      <button
        type="button"
        onClick={() => {
          setCount(count + 1);
        }}
      >
        count++
      </button>
    </div>
  );
}

function UseAwaitTest2() {
  const [count, setCount] = useState(0);

  const getUserName = (count) => () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          name: "Lee Seokkyu",
          count,
        });
      }, 5000);
    });
  };

  const [result1, result2] = useAwait(
    [getUserName(count), getUserName(count)],
    [count]
  );

  return (
    <div>
      <div>count : {count}</div>
      <div>name : {result1 ? result1.name : ""}</div>
      <div>name : {result2 ? result2.name : ""}</div>
      <button
        type="button"
        onClick={() => {
          setCount(count + 1);
        }}
      >
        count++
      </button>
    </div>
  );
}


function App() {
  return (
    <>
      <Suspense fallback={<div>...load data</div>}>
        <UseAwaitTest1></UseAwaitTest1>
      </Suspense>
      <Suspense fallback={<div>...load data</div>}>
        <UseAwaitTest2></UseAwaitTest2>
      </Suspense>
    </>
  );
}

Keywords

suspense

FAQs

Package last updated on 13 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts