Async/Await Promise Pending Error

Viewed 41861

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

1 Answers
Related