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

178
Views
Why i can't pass variables with my function?

I have this code in my react native app that is using firebase :

let bestTutors = [];

const getBestTutors = async () => {
    const snapshot = await getDoc(docRef);
    bestTutors = snapshot.data().list;
}

getBestTutors();

console.log(bestTutors);

But when i run it it only console logs "Array[ ]", I've tried to using .then( ) and all kind other stuff that doesn't make sense for me, idk if there's a wey to use it without the await or How can i pass the data to the variable :( ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

call function with await. Like this one

await getBestTutors()

about 4 years ago · Juan Pablo Isaza Report

0

Look at my comments

Your Code 1

let bestTutors = [];

const getBestTutors = async () => {
    const snapshot = await getDoc(docRef);
    bestTutors = snapshot.data().list;
}

getBestTutors(); // Asynchronous function

console.log(bestTutors); // this is called before getBestTutors() returns the data. So console.log(bestTutors) return de default value: []

The new code

let bestTutors = [];

const getBestTutors = async () => {
    const snapshot = await getDoc(docRef);
    bestTutors = snapshot.data().list;
}

await getBestTutors(); // Wait until getBestTutors() has finished and then Next.

console.log(bestTutors);

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!