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
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):
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With