Multiple API calls inside loop i am making multiple API calls inside the loop, function is called inside a promise and pass data to body this part work fine, in a function a return a promise which make multiple calls to API, before proceeding to further i have to get all the result. but when i console log the result it show empty array.
var rp = require('request-promise');
var watchonlinemovie = [];
exports.db = function(body){
return new Promise((resolve,reject)=>{
if(body.lenght > 0)
{
body.forEach(movie)
{
let title = movie.title;
title = (title.slice(0,title.indexOf('('))).trim();
let year = movie.title;
year = year.slice(year.indexOf('(')+1,year.indexOf(')'));
let call = "http://www.omdbapi.com/?t="+title+"&y="+year+"&apikey="+key2+"&plot=full";
rp(call)
.then((body) => {
let movie = JSON.parse(body);
watchonlinemovie.push({
title: movie.Title,
year:movie.Year,
rating:movie.Rated,
duration:movie.Runtime,
genre:movie.Genre,
director:movie.Director,
actors:movie.Actors,
image:movie.Poster,
href:movie.link,
});
})
.catch(err => {
reject(err);
});
}
}
resolve(watchonlinemovie);
});
}
body.length.