0

Axios get function always returning Promise even though I get the value in console.log

I use this code inside my vue js function

async encFolderID(_id){
        if(_id > 0){
            await axios.get(base_url+'Main/Process/FilesStorage/encFolderID/'+ _id).then(response => {
                return response.data.enc_id;
            })
        }
    },
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – jonrsharpe Aug 14 '21 at 07:43

2 Answers2

3

You don't need to use .then(). Just async and await.

This is how you should use them:

async encFolderID(_id){
        if(_id > 0){
            const resp = await axios.get(base_url+'Main/Process/FilesStorage/encFolderID/'+ _id);
            return resp.data.enc_id
        } else ...
    },
Micko Magallanes
  • 261
  • 1
  • 12
  • 2
    thanks for the answer. can I use the function like this? :id="encFolderID(folder.id)", I'm trying to set an ID using the function. –  Aug 14 '21 at 13:09
  • Yes, but you still have to use `await` before the `encFolderID()`. sorry for late reply – Micko Magallanes Aug 16 '21 at 09:10
0

Why you are using .then while you marked axios with await keyword? try this one. when using await you don't have to use .then

async encFolderID(_id){
        if(_id > 0){
            const res = await axios.get(base_url+'Main/Process/FilesStorage/encFolderID/'+ _id)
            return res.data.enc_id
        }
    },
nima
  • 71
  • 2
  • 7
  • 2
    hi is it possible to call a function using like this :id="encFolderID(folder.id)"? –  Aug 14 '21 at 13:10
  • 1
    you want to set the id using this function ? it should'nt be a problem if the function return successful data else you should do some error handling – nima Aug 14 '21 at 15:07
  • 2
    Yes, but it still returning [object promise] when callling it on :id –  Aug 15 '21 at 01:14
  • 2
    you should use `await` right before the encFolderID function – nima Aug 15 '21 at 06:33