简体   繁体   中英

Trouble with axios returning promise object

I imagine it's something really simple, but I'm having some trouble getting the correct response. My code is returning the promise object, and not the value.

My axios call is something like this:

export const myFunc = async (hash: string) => {
    return axios.get(`${url}/${path}?hash=hash`)
        .then((response: any) => {
            console.log('my response: ', response.data) // {key: value} as expected
            return response.data
        })
}

I call it from another file

const xy = async (c: string) => {
    return myFunc(c)
}

console.log('result of xy(): ' xy('some hash')) // result of xy(): { Promise <pending> } <--- ????

If I .toString() it, because I'm annoyed (and I think I had some reason why at one point but I don't remember what that is), I get

result of xy(): [object Promise]

I've googled, I've stack overflowed, and now I'm asking the question because what I've found so far doesn't quite work.

Thanks for you help

Explicit promise syntax fixed the issue. I'm sure I was missing something really simple. Thanks much to @EmileBergeron. But we also decided that the data didn't need to be encrypted at rest, so by storing this non sensitive data in an unencrypted state, we no longer needed to make a separate rest call to un-encrypt the hash and didn't need to worry about working in an additional promise in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM