Why does this http request get stuck on pending with async/await?

Viewed 20

So i'm getting some data from an external API for my web-app without any issues. However, these requests takes a bit too long so i'd like to cache this data once retrieved with the help of REDIS. I've managed to successfully do these steps separately but i need to be able to call the external API if there is no existing data in the redis-client.

I tried the /players GET request in Postman and it gets stuck on 'Sending request..'

app.get('/players', async (req, res) => {
const players = await getOrSetCache('players', async () => {
    return shl.players();
})
res.json(players);
})

Function to return a Promise. If there is no data, a callback is used to make the request to the external api and store that data in the redis-client

function getOrSetCache(key, callback) {
return new Promise((resolve, reject) => {
   redisClient.get(key, async(error, data) => {
          if (error) return reject(error)
          if (data != null) return resolve(JSON.parse(data))
          const freshData = await callback()
            redisClient.setex(key, 3600, JSON.stringify(freshData))
            resolve(freshData)
        })
    })
}

Callback code:

players() {
    return this.connection.get('/teams').then(teams => {
      const teamCodes = teams.map(team => team.team_code);

      Promise.all(teamCodes.map(teamCode => this.playersOnTeam(teamCode)))
        .then(teamData => {
          const teamInfo = [].concat(...teamData);
          const playerInfo = [].concat(...teamInfo.map(team => team.players.map(player => ({
            player_id: player.player_id,
            first_name: player.first_name,
            last_name: player.last_name,
            default_jersey: player.default_jersey,
            position: player.position,
          }))));
          
          return playerInfo;
          
        }
        );
    });      
}

Grateful for any tip of what the issue might be or if someone can spot errors in this implementation!

0 Answers
Related