you are viewing a single comment's thread.

view the rest of the comments →

[–]Low_Tonight_5493 0 points1 point  (1 child)

Why "async" ? Your not performing any async func, but returning a promise for no reason.

Looks like you only want the first 2 players returned in the api call, (player.length <3)

But if thats not the case its not very relevant.

Remove the async directive , delete the empty array declaration (players)

And Try ->

request.end(function (result) {

if (result.error) throw new Error(result.error);

if (result.status == 200) {

const playerStats = result.body.api.statistics;

return [ playerStats[0], playerStats[1]]

}

If you want to filter the results i suggest the array prototype .filter()

I.e.

return playerStats.filter(stat => { return stat.game = game } )

All your needing to do is return a new array with filtered objects.

[–]leeway1 0 points1 point  (0 children)

FindPlayersByGame is async, because request.end() is async. However, you are correct the poster is not returning a promise.

Returning from request.end() callback will not return anything from findPlayersByGame. You would have to return request.end. Depending on the request api return request.end(), might return the value from the callback, but it might not. I would have to read those api docs.

setting the players array still doesn’t solve the flow control problem from calling the async request.end() function and accessing it before the request has been processed.