Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

372
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!