Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async/Await Promise Pending Error

I'm having a bit of an issue with an error dealing with promises. So initially I had my function set up like this, and it all worked fine, no problems.

Original:

const request = require("request-promise");
async () => {
  const URL_HIDDEN = "...";

  let info = await request(URL_HIDDEN);
  info = JSON.parse(info).response.players[0];

  let playtime = await request(URL_HIDDEN);
  playtime = JSON.parse(playtime).response.games;

  console.log(info);
  console.log(playtime);
}

This is a shortened version, but basically I am just requesting information from the Steam API. Both the variables get print out with all the data no problem, but that changes once I do the refactor below.

Refactored:

const middleware = require("../middleware");
async () => {
  const URL_HIDDEN = "...";

  let requests = middleware.requestURI(URL_HIDDEN);
  console.log(requests);
}

and I started doing all the request handling in a separate function in a separate file.

const request = require("request-promise");
middlewareObj.requestURI = async (URL_HIDDEN) => {
  console.log("1");
  let info = await request(URL_HIDDEN);
  info = JSON.parse(info).response.players[0];

  console.log("2");

  let playtime = await request(URL_HIDDEN);
  playtime = JSON.parse(playtime).response.games;

  console.log("3");

  return [{ info }, { playtime }];
};

Once I got all the variables sorted out I want to return an array. However, once executed I get this printed to the console.

1
Promise { <pending> }
2
3

Any idea why it's printing promise pending once I refactored it?

In the original it prints out everything perfectly fine

like image 285
Phillip Avatar asked Aug 10 '17 08:08

Phillip


People also ask

Why does my promise say pending?

Why is my promise logging pending? The promise will always log pending as long as its results are not resolved yet. You must call . then on the promise to capture the results regardless of the promise state (resolved or still pending):

What happens if promise is not resolved in await?

If the promise is rejected, the await expression throws the rejected value. The function containing the await expression will appear in the stack trace of the error. Otherwise, if the rejected promise is not awaited or is immediately returned, the caller function will not appear in the stack trace.

Can we use promise with async await?

async and await Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.


1 Answers

in your "Refactored" code -

let requests = middleware.requestURI(URL_HIDDEN);

without an await, requests will be a pending promise - so, you need

let requests = await middleware.requestURI(URL_HIDDEN);

To expand on this. Any function tagged async will return a Promise.

async function foo() {
    return true;
}

calling this like

let bar = foo();
// here bar = pending promise of foo()

Results in bar being the Promise

calling it like

let bar = await foo(); 
// here bar == true

is sort of equivalent (but nowhere near equal) to

foo().then(bar => {
    // here bar == true
})

async/await can make code that is already simplified by Promise's even simpler - in this contrived example, it's not obvious though

like image 182
Jaromanda X Avatar answered Oct 05 '22 22:10

Jaromanda X