2

I am trying to use an async function to call a function inside another function. It looks like this:

const getConnectionsWithEmailsHash = async () => {
    const connectionsWithEmails = await parseConnections('./tmp/connections.csv') 
    console.log(connectionsWithEmails)
    return connectionsWithEmails
}

const connectionsWithEmailsHash = getConnectionsWithEmailsHash()
console.log(connectionsWithEmailsHash) 

When I console.log() inside the async function, I get the hash I am expecting, but when I console.log() the result of calling the async function, I get promise pending. I though the point of an async function was that it waits for the promise to be resolved when it is called, so what I am I doing wrong?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
adam tropp
  • 674
  • 7
  • 22

3 Answers3

5

async functions return promises. This line:

const connectionsWithEmailsHash = getConnectionsWithEmailsHash()

...just sets connectionsWithEmailsHash to the promise that the function returns. To actually get the resolution value of the promise, you'd need to:

  1. Use await within another async function (if that means using async at the top level, see: How can I use async/await at the top level?):

    const connectionsWithEmailsHash = await getConnectionsWithEmailsHash()
    

    or,

  2. Use then on the promise

    getConnectionsWithEmailsHash()
    .then(connectionsWithEmailsHash => {
        // ...use `connectionsWithEmailsHash`....
    })
    .catch(error => {
        // ...handle error...
    })
    
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

getConnectionsWithEmailsHash is still an async function itself. connectionsWithEmails is valid because you awaited parseConnections, but connectionsWithEmailsHash is not valid because getConnectionsWithEmailsHash is running in parallel. Try "await getConnectionsWithEmailsHash".

Now, if you want to use this on the top level, that's another question. That question is answered here.

Nicholas Pipitone
  • 4,002
  • 4
  • 24
  • 39
  • So then I would have to wrap it in another async function? I thought await could only be called inside an async function? – adam tropp Sep 21 '18 at 22:32
  • Well, logically, if you did do that then you'd freeze up the main thread. JS prevents that for sanity reasons, but it's beneficial to understand why JS does that. You don't want top-level awaits even if the language allowed it. – Nicholas Pipitone Sep 21 '18 at 22:33
  • @adamtropp Eventually you need to resolve the promise that comes from your highest async function somehow - e.g. through a try/catch or then/catch block. – Mark Sep 21 '18 at 22:38
0

I think you don't need a wrapper function. const connectionWithEmailHash = await parseConnections(arg);

This should work for given code in question.

Code snippet in question won't work since async function should return a promise. So try returning a promise in getConnectionWithEmailHash that resolves with connectionsWithEmails and your code should work.

Shubham Gupta
  • 2,596
  • 1
  • 8
  • 19