I want to return promise.all() with sources variable locally defined below so i could consume the promise when i call sourceRequest.then((sources)=>{//do something}) here's my code:
var sourceRequest=(...countries)=>{
var sources=[];
var promisesCollection=[];
for(var country of countries)
{
var promise=this._vm.$axios.get('/sources',{params:{ country,language:'en'}})
.then(response=>{
sources.push(response.data.sources);
});
promisesCollection.push(promise);
}
Promise.all(promisesCollection).then(()=>{
return new Promise((resolve)=>resolve(sources));
})
};
sourceRequest('in').then((sources)=>console.log(sources));
The problem is i get undefined in console The workaround: i have a workaround to this but the problem is i want the function to return sources and not api requests responses:
//this works
var sourceRequest=(...countries)=>{
var promisesCollection=[];
for(var country of countries)
{
var promise=this._vm.$axios.get('/sources',{params:{ country,language:'en'}})
promisesCollection.push(promise);
}
return Promise.all(promisesCollection);
};
sourceRequest('in').then((response)=>{console.log(response)});