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

ottaviane's avatar

Pinia and Vue3: promise then and catch .

Hi all. I'd like to create a method in pinia that works like a promise so that when I call it I can do this:

myStore.init().then({
    //my code
}).catch({
  //my error code
});

How can I do it? In pinia store I try to do it:

export const UseAuthStore = defineStore('auth', {
  
  actions: {  
    init(){
        return new Promise((resolve,reject) => {
            axios.get("auth",{}).then(resp => {               
                        resolve(resp);
                    }).catch(err => {                       
                        reject(err);
                    });                   
        })
    },
 
})

is it the right mode to do it? thanks Hi all

0 likes
1 reply
MohamedTammam's avatar
Level 51

is it the right mode to do it?

Yes. However, you can just write the following

actions: {
  init(){
    return axios.get("auth");
  },
}
1 like

Please or to participate in this conversation.