Node version: 8.1.3
Hey guys,
I will try to make this as simple as possible =)
I have a daily .xlsx report with stats fetched from an API. it was working smoothly. However, at some point I had to move from Promise.all to resolving Promises in a sequence fashion, given the fact that API was throwing a considerable number of "Under heavy load" errors when all calls were made in parallel and I did not want to program any concurrency functions for that.
So far so good.
First problem -> for one part of the data API does not return me exactly what I want. I need Stats aggregated by, let´s call type and day. However, API can´t aggregate both at the same time, so I have to go on a loop and request the type aggreg. for each day I need.
Second problem -> API won´t return in the JSON which day the request was made for, so I have assign it to the object in the then() statement
for (var date = moment(startDate); date.isBefore(yesterday); date.add(1, 'days')) {
promiseStats.push((() => {
var report = rep;
var requestDate = date.format('YYYY-MM-DD');
return getStats(adv.id, {
dayFrom: date.format('YYYY-MM-DD'),
dayTo: date.format('YYYY-MM-DD'),
groupBy: 'campaign'
}).then((res) => {
_.each(res, (o) => {
Object.assign(o, {
day: requestDate
})
});
report.data.push(...res);
}); // getCampaignStats()
})); // end promiseStats.push
} // for date end
I created an anonymous function to scope the variable and be able to access it when resolving the Promise. This was working nicely when I was using Promise.all . However, when I changed the code to resolve in sequence, it, of course, stopped working, since code only run that anonymous function when the promise will be resolved and by then, date is not the same value.
This is how I´m resolving promises now:
promiseStats.reduce((p, fn, index) => {
return p.then(val => {
return fn();
});
}, Promise.resolve()).then(() => {
//do stuff here
});
Question Is there a way to go around this and have the correct date value assigned to the object? This has been a stumbling block for me for some days already. I think I could solve this by going back to old code and use concurrency and balance the number of calls to avoid overloading API.
Thanks a lot in advance!