Transform this to one line arrow function ES6+
First of all, onAuthStateChanged expects a callback and can be fired multiple times. To resolve that you can use unsubscribe() after the first success.
Secondly, for your code to work, you need to wait for signInAnonymously. At the moment you assign it to a temporary variable, but I can't see it working. Because onAuthStateChanged accepts a callback that can be fired multiple times, it won't wait in the main scope. To solve this, I suggest using Promise constructor.
Finally, you call signInAnonymously, but you don't do anything with its response. You need to use user.uid if the user is logged in, otherwise you need to use signInAnonymously().user.uid
So the final solution:
const getUID = async () =&gt; new Promise(resolve =&gt; {
    const unsubscribe = firebase.auth().onAuthStateChanged(user =&gt; {
        unsubscribe();
        resolve(user ? user.uid : firebase.auth().signInAnonymously().then(v =&gt; v.user.uid))
    });
})

const uid = await getUID()

Explanation:

Wrap everything to Promise and resolve it when we get uid
Auth &amp; listen on onAuthStateChanged
Unsubscribe on the first onAuthStateChanged callback trigger (because one trigger it's enough in your case)
Check if a user is defined (that means the user is signed in), resolve promise with user uid
If the user is not defined, use signInAnonymously to sign anonymous user, resolve that user `uid.