Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

373
Vistas
Async / Await - Order of Operations not working

Overview

I am working in a react app using redux. I have an action that checks wether or not we have new image data. If so, it will upload it, receive the new URL and then use this data to update the object.

Problem

However, the order of operations within my function are working as expected, however code outside the function runs before it is completed.

Question

Why is my console log at the bottom executing before the contents of my async function are completed?

if(imageData) {
      const imageGUID = guid();
      const storageRef = projectStorage.ref(`${imageData.name}_${imageGUID}`);
      // This function should complete before the console log at the bottom is called.
      await storageRef.put(imageData).on('state_changed', snap => {
        }, async (err) => {
          console.log(err)
          toastr.error("Uh Oh!", `Could not upload image`);
        }, async () => {
          imageURL = await storageRef.getDownloadURL();
          console.log("NEW IMAGE URL: ", imageURL);
          
      })
    }
    console.log("Done setting up new image: ", imageURL) // This is called before we get the IMAGE URL from Firestore.... why?
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The .on function does not return a Promise so there is nothing to wait for the await. You have to convert the event base API of put to a Promise.


if (imageData) {
  const imageGUID = guid();
  const storageRef = projectStorage.ref(`${imageData.name}_${imageGUID}`);
  // This function should complete before the console log at the bottom is called.
  try {
    await new Promise((resolve, reject) => {
      storageRef.put(imageData)
        .on('state_changed', snap => {},
          reject, resolve)
    })

    imageURL = await storageRef.getDownloadURL();
    console.log("NEW IMAGE URL: ", imageURL);
  } catch (err) {
    console.log(err)
    toastr.error("Uh Oh!", `Could not upload image`);
  }
}
console.log("Done setting up new image: ", imageURL)
about 4 years ago · Juan Pablo Isaza Denunciar

0

storageRef.put(imageData).on - doesn't look like promise (you upload your image in callback), so await doesn't make sense

If you want to use promises, you should write it something like that

 await new Promise((resolve, reject) => {
      storageRef
        .put(imageData)
        .on('state_changed', snap => {
        }, async (err) => {
          console.log(err);
          toastr.error('Uh Oh!', `Could not upload image`);
          reject()
        }, async () => {
          imageURL = await storageRef.getDownloadURL();
          console.log('NEW IMAGE URL: ', imageURL);
          resolve()
        });
    })
about 4 years 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 vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda