'How to call an async cleanup function?

How can I call an async cleanup function in useEffect?

useEffect(() => {
    return () => Voice.destroy().then(Voice.removeAllListeners);
  }, []);

The EffectCallback expects void, not Promise<void>



Solution 1:[1]

You can wrap the body in curly braces, because currently it returns the Promise (this is what happens when you omit the curly braces, a one line return), which makes your cleanup function return a Promise<void>. With curly braces, it will be just the body of the function and not an implied return statement:

useEffect(() => {
    return () => {
           Voice.destroy().then(Voice.removeAllListeners)
        };
  }, []);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ron B.