Before initializing a React App, I fetch the user's info in the localStorage. Then, thanks to his id, I start a socket connection with the server.
When passing the user's id to the socket function, typescript claims that:
Argument of type 'Promise' is not assignable to parameter of type 'string'
Here is my index.tsx file:
const user = fetchUser();
if (user) {
getNotifications(user);
}
ReactDOM.render(
<App />,
document.getElementById("root")
);
And the fetchUser() function:
export async function fetchUser() {
try {
if (!localStorage.getItem("MyAppName")) return null;
const user = await jwt_decode(localStorage.MyAppName);
updateUser(user);
return user.id;
} catch (err) {
return null;
}
}
How to fix this? If I remove the async/await it works, but performance-wise, is it ok to block the thread like this before initializing the app?