Skip to content

justinobney/async-lifecycles

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Async lifecycles

Utility function to reduce branching of async function states

Install

yarn add async-lifecycles

Reasoning

This was primarily born out of wanting something better with react and setState though not tied to react at all. I found myself repeating code like this:

// ...
someAsyncCall = async () => {
  this.setState({
    loading: true,
    error: null
  });
  try {
    let data = await api.getData("...");
    this.setState({
      loading: false,
      data: data,
      error: null
    });
  } catch (error) {
    this.setState({
      loading: false,
      error: error
    });
  }
};
// ...

Instead of littering my code with all the try/catch or promise.then(/*...*/).catch(/*...*/), I found it more pleasant to have code like so:

// ...
someAsyncCall = async () => {
  asyncLifecycles(
    () => api.getData("..."),
    ({ loading, data, error }) => {
      this.setState({
        loading,
        error,
        data
      });

      if (data) {
        // do something on success if you choose
      }
    }
  );
};
// ...

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published