Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

376
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda