question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Guidance for async effects?

See original GitHub issue

Our team is currently using redux-loop for handling asynchronous side-effects kicked off by our reducer. We’re really excited about use-methods, and we’re trying to figure out if there’s any guidance on how to initiate async calls and have those trigger state updates when they complete.

cc @alexburner

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
pelotomcommented, Mar 27, 2019

Hey @TedDriggs, if all you’re doing is initiating a single fetch on component mount, this is straightforward to do with just useEffect:

const [state, { receiveData }] = useMethods(methods, initialState);

useEffect(() => {
  let alive = true;
  fetchData().then(data => alive && receiveData(data));
  return () => { alive = false; };
}, []);

If you might want to fetch multiple times in the course of the life of the component it gets more complicated, and you probably want to do things like debouncing. There are a lot of interesting hook-based solutions emerging for managing async flows; it’s very early for this space and I think it’s an open question what the right direction is, or whether there will ever be a one-size-fits-all answer to the problem. I’d recommend browsing through here: https://nikgraf.github.io/react-hooks/

useMethods is really only concerned with solving one problem well, which is managing synchronous state. Whatever async solutions you arrive at, useMethods should work well with them 😄

3reactions
ja0nzcommented, Mar 28, 2019

useMethods is very pluggable. Async effects can be modeled like normal methods. In my implementation I use a wrapper to create async methods:

const withAsync = ([states, methods]) => {
    const { receiveData } = methods;
    return [
        states,
        {
            ...methods,
            async fetchData() {
                const data = await fetch();
                receiveData(data);
            }
        }
    ]
};

const [
    { data }, // <- latest state
    { receiveData, fetchData }, // <- callbacks for modifying state
] = withAsync(useMethods(methods, initialState));

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async/Await - Best Practices in Asynchronous Programming
The guidelines are summarized in Figure 1; I'll discuss each in the following sections. Figure 1 Summary of Asynchronous Programming Guidelines ...
Read more >
AspNetCoreDiagnosticScenarios/AsyncGuidance.md at master
This repository has examples of broken patterns in ASP.NET Core applications - AspNetCoreDiagnosticScenarios/AsyncGuidance.md at master ...
Read more >
Async Code in useEffect is Dangerous. How Do We Deal with It?
However, we have not considered that the asynchronous workflow in the first useEffect is running concurrently to the rest of the application, ...
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
An async function can handle a promise called within it using the await operator. await can be used within an async function and...
Read more >
How to embrace asynchronous communication for remote work
Here is a complete guide to everything you need to know about how to work and communicate asynchronously in a remote work environment....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found