Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gabotronix's avatar

Async function inside mobx-store not awaiting when called inside my components

in my react native components I call async functions defined inside my mobx stores, however when I call them the code after them inmediatly runs instead of waiting for the async function to end.

In my case "ACTION COMPLETED" appears on console before the axios call is completed...

I call it like this inside my components:

const sendVerify = async () =>
    {
        try 
            {
                //Tried with await
                await root.userStore.getLoggedUser(response.data.user.id);
                console.log('ACTION COMPLETED');
                //Also tried with then at didn't work
                root.userStore.getLoggedUser().then(response => console.log('ACTION COMPLETED'));
               
            } 
            catch (error)
            {
                throw error;
            }
    };

Async function inside UserStore:

export class UserStore
{
    root = null;

    
    constructor(root) 
    {
        makeAutoObservable(this, { root: false });
        this.root = root;
    }

async getLoggedUser(user_id = false)
    {
  
          try
          {
              let url = env.BASE_URL+'/api/auth/sanctum/user';
  
              let params = {};

              
              let response = await http.get(url, {params:params});
              
          
          return true;
          
      }
      catch (error)
        {
            return false;
            throw error;
        }
}
0 likes
0 replies

Please or to participate in this conversation.