What should be done if vue passes different parameters to get different values?

//api.commonItem 
getCommonItem (value) {
        let variable = ""
        api.commonItem({value: value}).then(res => {
          variable = res.data.name
          console.log("",variable)
        })
        console.log("",variable)
        return variable
      },

clipboard.png

I called the getCommonItem method twice and passed the parameters twice. I wanted to get a different value, but the second time it was empty. Is there something wrong with the logic? please help me to see if the request has not been completed. Variable has been return

.
Mar.16,2021

if you encapsulate commonItem , you should know that a Promise is returned. This is an asynchronous operation. If you want variable to be obtained by getCommonItem callers, either change it to the classic callback :

.
getCommonItem(value) {
    return api.commonItem({value: value}).then(res => {
        let variable = res.data.name
        console.log("",variable)
        return Promise.resolve(variable)
    })
}

getCommonItem(...)
    .then(variable => {
        console.log('', variable)
    })
Menu