-1

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

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Coder-guy
  • 414
  • 6
  • 16
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Emile Bergeron Jan 13 '20 at 19:11
  • JavaScript is an highly asynchronous language. There are different ways to handle asynchronous operations, like callbacks which were popular in the earlier (and still exist) and now, [_Promises_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). – Emile Bergeron Jan 13 '20 at 19:12
  • I'm using async functions above, and when I add the await (`return await axios.get...` or `return await myFunc...` I get the same results. I could try the explicit promise syntax, but we are hoping to move from that to async await throughout our code. @EmileBergeron – Coder-guy Jan 13 '20 at 19:15
  • 1
    You can't get away from Promises, [`async/await` is just syntactic sugar on top of Promises](https://stackoverflow.com/q/53057110/1218980). At some point, you'll have to deal with the async nature of JS, so you'd better learn it today ;) – Emile Bergeron Jan 13 '20 at 19:17

1 Answers1

1

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.

Coder-guy
  • 414
  • 6
  • 16