Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

77
Vistas
Resolve multiple network calls at once in react

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)

image.pngImgae of chunked queries

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

7 months ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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.

7 months ago · Juan Pablo Isaza Denunciar

0

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)
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.