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

146
Views
How to await a promise inside a firebase get request

I am trying to get the owner variable and since the function usually returns it as undefined I decided to use async/await but I can't use await within another function and I can access the variable outside of it. Is there an easy solution or do I have to rework the function?

async getOwner() {
        database.collection("users").doc(this.owner).get().then(data => {
            var owner = new Promise(function (resolve, reject) {
                resolve(new User(data.data().username, "", data.data().email, [], data.data().info))
            })
            let result = await owner
            return result
        })
    }

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

0

If you're using async/await, there is almost never a need to use then() or new Promise(). You can write simply this, awaiting the promise returned by get():

async function getOwner() {
    const snapshot = await database.collection("users").doc(this.owner).get()
    const data = snapshot.data()
    return new User(data.username, data.email, [], data.info)
}

You should probably also write code to check if there was any data in the snapshot and decide what to do if the document was not found as seen in the documentation.

You might want to review how async/await works in order to use it effectively.

https://javascript.info/async-await

about 4 years ago · Juan Pablo Isaza Report

0

While Doug's answer is the better solution (hence my upvote on it), I wanted to point out why your current approach doesn't work, which is because you're not returning anything from the top-level code.

So if you want to not use await, the closest to get your current code working would be:

function getOwner() {
    // 👇 Add return here
    return database.collection("users").doc(this.owner).get().then(data => {
        var owner = new Promise(function (resolve, reject) {
            resolve(new User(data.data().username, "", data.data().email, [], data.data().info))
        })
        let result = await owner
        return result
    })
}

And the simpler version, getting rid of the extra code:

function getOwner() {
    return database.collection("users").doc(this.owner).get().then(doc => {
        return new User(doc.data().username, "", doc.data().email, [], doc.data().info)
    })
}

The top-level return is needed, because it allows the nested return to "bubble up" and back to the code that calls getOwner.

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!