question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async / Await returning Promise <pending> instead of values

See original GitHub issue

Hi,

There seems to be an issue with Async/Await handling. Code below:

async function getData() {
  console.log('logging');
  const test = await CSVToJSON().fromFile('./data/hans-puns.csv');

  return test;
}

const data = getData();

console logging data shows a Promise

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:21
  • Comments:21

github_iconTop GitHub Comments

95reactions
splichtecommented, Oct 4, 2019

I think I might have been unclear in my earlier comment. I never wanted you to try to await outside an async function. You can’t do that in current Node. What you need to do is wrap your await call inside an async function, and then call that async function in the top-level of your script. If you just try to immediately use the output of the async function, it isn’t going to be useful, because it’s going to be a Promise. But I think people got the impression that I was suggesting to use the given code as-is but only adding await, which is not what I meant to say at all.

You’re trying to do something like this:

const data = asyncFunc();
console.log(data); // will give you something like Promise { }

You read my comment and understandably thought I meant to do this:

const data = await asyncFunc(); // will error
console.log(data);

What I actually meant was to do this:

async function run() {
  const data = await asyncFunc();
  console.log(data); // will print your data
}

run() // run the async function at the top-level, since top-level await is not currently supported in Node

You don’t need to await on the final run() call, since Node won’t exit until its event loop is empty.

85reactions
splichtecommented, Sep 24, 2018

async functions return promises. you need to do const data = await getData() – unless you intend to set data at the top level of the code, in which case you can’t await on getData and need to console.log from inside getData, or log from a different function that calls await getData() inside it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my asynchronous function returning Promise ...
The promise will always log pending as long as its results are not resolved yet. You must call .then on the promise to...
Read more >
asynchronous function returning promise pending instead of a ...
In the below code I am making a Server Call using a async/await. Once I get the results I ...
Read more >
Promise <pending> - Damaris Göbel
A promise represents a single asynchronous operation that hasn't been completed yet, but is expected in the future.
Read more >
Async Function Returns Promise <pending> : r/learnjavascript
Async functions always return promises. then()'s also always return promises. If you are trying to access a value from an async function there's ......
Read more >
Why is my asynchronous function returning Promise { pending ...
JavaScript : Why is my asynchronous function returning Promise { pending } instead of a value ? [ Gift : Animated Search Engine ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found