@r3dev/lets-wait

0.3.0 • Public • Published
This simple API allows you to manually control named tasks/processes as promises, in an imperative manner.
Example with React:
function SomeComponent() {
  const { login, user } = useLogin();
  const [loading, setLoading] = useState(false);

  async function loginWithData() {
    lets.start("login.user.data");
    setLoading(true);
    await login(); // this login function doesn't fetch all component required data
    await lets.waitFor("login.user.data");
    setLoading(false);
  }

  useEffect(() => {
    if (user.data) {
      lets.finish("login.user.data");
    }
  }, [user]);

  //...
}

This is just a random example and not a good example of best practices.

Api

  • lets.start(processID: string) - Start or restart a named process
  • lets.waitFor(processID: string): Promise - Returns a promise of a previously started process (instantly rejected if the process does not exist).
  • lets.finish(processID: string): Boolean - Terminates a previously started process and resolve its promise (returns true if the process was found and terminated).

Package Sidebar

Install

npm i @r3dev/lets-wait

Weekly Downloads

0

Version

0.3.0

License

ISC

Unpacked Size

5.73 kB

Total Files

7

Last publish

Collaborators

  • r3dev