Skip to content
Advertisement

Calling async function in .map() (React)

I’m trying to iterate over IDs (track.id) and for each of them fetch some data (based on those IDs) from API (getBPM()). I’m pretty sure it’s connected with Promises and I’m not very fluent with these yet. Here’s the code:

JavaScript

I’ve tried some things, but couldn’t get it to work. I’d appreciate any kind of help

Advertisement

Answer

When you call an async function you have to await the the function like await getBpm(track.id). Ideally you shouldn’t fetch the data in the return statement. the spotify API allows you to fetch multiple tracks on each request. https://developer.spotify.com/console/get-audio-features-several-tracks/ I would do something like

JavaScript

Then you might want to merge your 2 data sets. And add your tempo-data to your tracks-variable.

And your function.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement