W
W
webe2018-03-07 03:26:15
JavaScript
webe, 2018-03-07 03:26:15

How to fix promises?

There is an API where I get an array of movies, each movie has an ID (let's say we have 10 movies)
We need to go through this array and send each movie ID to another API, after I get information about the movie, I need to update the current element of the array.
In short, in 1 array we just have the names and IDs of films, we need to color information on films from another source to it.
app.get("/", async (req, res) => {
console.clear();
let videos = await getVideos();//function returns a promise
videos.items = videos.items.map(async (el) =>{
el.onfo= await getChannelInfo(el['id'].channelId); //function returns a promise
return el;
})
Something goes wrong here :(
in videos I get a list of films, everything is ok.
But when I try to modify each element of the array, something goes wrong (videos.items contains promises, not data)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2018-03-07
@webe

My guess is that the getVideos function returns a Promise for each element because the array is not then wrapped in Promise.all. One possible solution to the problem:
let videos = await Promise.all(getVideos());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question