Home > other >  How to get value back from vue method
How to get value back from vue method

Time:03-25

I am trying to make a Vue.js app and have the below method. I want to have conversation be returned so I can do the code below that calls it and access the result. My question is how do I make my method return back the value I need? It shows as a promise in the chrome console. Should I make the method into a promise to use .then on it?

method

createConversation(data) {
   let client = data.client
   let conversation = null
   client.on('stateChanged', async (state) => {
   if (state === 'initialized') {
        conversation = client.getConversationBySid(data.sid)
        return conversation
     }
   })
},

code that calls method and how I want to get value back to be used

const fetchedConversation = await this.createConversation({
    sid: conversation.conversation_sid,
    client: conversationsClient
})
console.log(fetchedConversation)

CodePudding user response:

createConversation(data) {
   let client = data.client
   let conversation = null
   return new Promise((resolve, reject)=> {
     client.on('stateChanged', (state) => {
       if (state === 'initialized') {
          conversation = client.getConversationBySid(data.sid)
          resolve(conversation)
       }
     });
     // depends how client throw err
     client.on('error', (err)=> reject(err));
   })
}
  • Related