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

143
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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