I want to download time-series data from a rest service. Currently, I have the following implementation
async function getTimeSeriesQuery(i) {
// Using this to show how gql is being used
appollo.query(getChunkQueryOptions(i))
}
var promises = []
for(var i = 0; i < numberChunks; i++) {
promises.push(getTimeSeriesQuery(i))
}
const data = Promise.all(promises)
I expected the promises to take the same amount of time as one promise but it seems to scale with the number of promises I have in the list of promises
The exection time of each promise has nothing to do with number of promises on the list nor with the exection time of another promise.
Promise.all doesn't begin Promises execution, it just waits for them to finish. If all of the Promises already were resolved earlier (for example if all HTTP requests already have finished), then the Promise.all will resolve to a value almost immediately, because there is simply nothing to wait for anymore. Promise.all, gives in a list the results of each promise.
Example : We have a liste of 2 promises : - 1 takes 10s - 2 taked 1s Promise.all will take 10s.
As mentioned, the duration of each request cannot be determined beforehand.
If you want, though, to wait for all to complete, you need to add the promises in your array (which the snippet you posted does not). And if you want the data in a variable, you will need to either use the .then
or await
for the results.
So you need to return the actual promise from your getTimeSeriesQuery
async function getTimeSeriesQuery(i) {
// Using this to show how gql is being used
// was missing the return
return appollo.query(getChunkQueryOptions(i))
}
var promises = []
for(var i = 0; i < numberChunks; i++) {
promises.push(getTimeSeriesQuery(i))
}
const data = await Promise.all(promises)