Async code in useEffect is dangerous, how do we deal with it?

The introduction of async/await to Javascript has made it easy to express complex workflows that string together multiple asynchronous tasks. Let’s take a look at the following code, which is a generalized example of code I’ve seen in real projects:


This content originally appeared on DEV Community and was authored by Jae Anne Bach Hardie

The introduction of async/await to Javascript has made it easy to express complex workflows that string together multiple asynchronous tasks. Let's take a look at the following code, which is a generalized example of code I've seen in real projects:

const useClient = (user) => {
  const [client, setClient] = useState(null);

  useEffect(() => {
    (async () => {
      const clientAuthToken = await fetchClientToken(user);
      const connection = await createWebsocketConnection();
      const client = await createClient(connection, clientAuthToken);
      setClient(client);
    })();
  }, [user]);

  useEffect(() => {
    return () => {
      client.disconnect();
    };
  }, [client]);

  return client;
};

It's easy to look at that and think it's all rosy. When we are passed a user we create a client for them and then whenever a client is disposed of through the user changing or the component unmounting, we disconnect the client.

However, we have not considered that the asynchronous workflow in the first useEffect is running concurrently to the rest of the application, which is independently responding to other effects and user actions. Any one of those other effects could unmount our component at any point! If the component is unmounted before setClient is called the client will still be created — Promises do not get cancelled just because their caller no longer exists — but without a component to manage the state setting or cleanup it will never disconnect. This is usually quite bad.

So what do we do about it? Well, it's complicated. At first glance it looks like we can do the following and things will be OK:

const useClient = (user) => {
  const [client, setClient] = useState(null);

  useEffect(() => {
    let client;

    (async () => {
      const clientAuthToken = await fetchClientToken(user);
      const connection = await createWebsocketConnection();
      client = await createClient(connection, clientAuthToken);
      setClient(client);
    })();

    return () => {
      client?.disconnect();
    };
  }, [user]);

  return client;
};

Now if the client has been created it will disconnect, without it needing to be saved to component state. Right?

Wrong, unfortunately. If the cleanup function runs before createClient resolves there will be no client to clean up. However, the promise is still resolving and the client will be created, once again putting it outside our reach!

If we really want to be able to safely use async workflows inside useEffect we need to make our workflow cancellable at any point. We also need to reason through what needs to be cleaned up depending on what stage the workflow was in when the interruption arrived. Here is an example:

const useClient = (user) => {
  const [client, setClient] = useState(null);

  useEffect(() => {
    let cancelled;
    (async () => {
      const clientAuthToken = await fetchClientToken(user);
      // if cancelled before we get to creating resources
      // it's ok, just don't create them
      if (cancelled) return;

      const connection = await createWebsocketConnection();
      // if cancelled before we get to the client, we need
      // to make sure our connection isn't left hanging
      if (cancelled) {
        connection.close();
        return;
      }

      const client = await createClient(connection, clientAuthToken);
      // if cancelled after the client has been created, we
      // need to clean it up
      if (cancelled) {
        client.disconnect();
        return;
      }

      setClient(client);
    })();

    return () => {
      cancelled = true;
    };
  }, [user]);

  useEffect(() => {
    return () => {
      client.disconnect();
    };
  }, [client]);

  return client;
};

If you're struggling to understand where to put cancellation handlers, imagine you were writing this with promises instead of async/await. We have to handle cancellation at the beginning of every .then callback:

const useClient = (user) => {
  const [client, setClient] = useState(null);

  useEffect(() => {
    let cancelled;
    fetchClientToken(user).then((clientAuthToken) => {
      if (cancelled) return;

      createWebsocketConnection().then((connection) => {
        if (cancelled) {
          connection.close();
          return;
        }

        createClient(connection, clientAuthToken).then((client) => {
          if (cancelled) {
            client.disconnect();
            return;
          }

          setClient(client);
        });
      });
    });
    return () => {
      cancelled = true;
    };
  }, [user]);

  useEffect(() => {
    return () => {
      client.disconnect();
    };
  }, [client]);

  return client;
};

The above is why I sometimes shy away from async/await in UI code entirely. The async/await syntax blurs the line between synchronous (not interruptible) and asynchronous (interruptible) code. That's the point! It is very helpful in contexts where synchronous and asynchronous code should be treated similarly — like in a backend server executing a linear workflow — but dangerously misleading in contexts where interruptions are common and handling them explicitly becomes necessary.

There are, of course, more sophisticated ways of dealing with the problem of resource management that make the implicit state machine above more explicit and controllable. I will leave an implementation in xstate as an exercise for the reader but it's one example of a useful tool to reason through and model these multi-step interruptible processes. However, it's good to have a barebones, just-React solution in your back pocket in case you find yourself unexpectedly facing a dangerous Promise in a foreign project.


This content originally appeared on DEV Community and was authored by Jae Anne Bach Hardie


Print Share Comment Cite Upload Translate Updates
APA

Jae Anne Bach Hardie | Sciencx (2021-10-20T23:07:53+00:00) Async code in useEffect is dangerous, how do we deal with it?. Retrieved from https://www.scien.cx/2021/10/20/async-code-in-useeffect-is-dangerous-how-do-we-deal-with-it-2/

MLA
" » Async code in useEffect is dangerous, how do we deal with it?." Jae Anne Bach Hardie | Sciencx - Wednesday October 20, 2021, https://www.scien.cx/2021/10/20/async-code-in-useeffect-is-dangerous-how-do-we-deal-with-it-2/
HARVARD
Jae Anne Bach Hardie | Sciencx Wednesday October 20, 2021 » Async code in useEffect is dangerous, how do we deal with it?., viewed ,<https://www.scien.cx/2021/10/20/async-code-in-useeffect-is-dangerous-how-do-we-deal-with-it-2/>
VANCOUVER
Jae Anne Bach Hardie | Sciencx - » Async code in useEffect is dangerous, how do we deal with it?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/20/async-code-in-useeffect-is-dangerous-how-do-we-deal-with-it-2/
CHICAGO
" » Async code in useEffect is dangerous, how do we deal with it?." Jae Anne Bach Hardie | Sciencx - Accessed . https://www.scien.cx/2021/10/20/async-code-in-useeffect-is-dangerous-how-do-we-deal-with-it-2/
IEEE
" » Async code in useEffect is dangerous, how do we deal with it?." Jae Anne Bach Hardie | Sciencx [Online]. Available: https://www.scien.cx/2021/10/20/async-code-in-useeffect-is-dangerous-how-do-we-deal-with-it-2/. [Accessed: ]
rf:citation
» Async code in useEffect is dangerous, how do we deal with it? | Jae Anne Bach Hardie | Sciencx | https://www.scien.cx/2021/10/20/async-code-in-useeffect-is-dangerous-how-do-we-deal-with-it-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.