At the moment what I have is this :
getData(user) {
return this.$q.all({
userInfo: this.$http.get(this.buildUrl('user.getinfo', user)),
userTopArtists: this.$http.get(this.buildUrl('user.gettopartists', user)),
userChart: this.$http.get(this.buildUrl('user.getWeeklyChartList', user))
.then(resp => {
let promises = [];
let data = resp.data.weeklychartlist.chart;
for (let i = data.length - 4; i < data.length; i++) {
promises.push(
this.$http.get(this.buildUrl('user.getWeeklyTrackChart', user) + `&from=${data[i].from}&to=${data[i].to}`)
.then(resp => {
return resp.data.weeklytrackchart.track.length;
})
);
}
return this.$q.all(promises);
})
}).then(resp => {
return resp;
}).catch(err => {
this.$q.reject('Error' + err.status);
})
}
But I'm thinking there may be a more functional approach to building this promise object, because a lot of code is getting repeated. So I tried to come up with a better solution:
buildPromiseObj(target) {
const methods = ['getinfo', 'gettopartists', 'getweeklychartlist'];
let obj = {};
for ( let method in methods ) {
obj[`${methods[method]}`] = this.$http.get(this.buildUrl(`${target}.${methods[method]}`, target))
}
}
- Does this approach make sense and should I use it ?
- You can see that the 3rd request I make in the first function has other nested requests. Is there any way I can integrate that in the approach I've written above ?