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

94
Vistas
Async / await code not called in useEffect function

I can't figure out why the await function setDoc is not completed in my example below when I launch my react native app for the first time.

When I launch it a second time however, it works well.

Can you help me?

useEffect(() => {

    registerForPushNotificationsAsync().then(async token => {
      
      // The following gets called
      console.log("Before await")

      // The following does not complete when I first launch the app.
      await setDoc(doc(db, "devices", token), { test: "test" })
        .then(x => {
          // The following does not get called
          console.log('Sucess')
          
        })
        .catch(error => {
          // The following does not get called
          console.log('Error')
        })

      // The following does not get called
      console.log("After await")
    });

    return () => {};
  }, []);

with registerForPushNotificationsAsync defined outside useEffect as:

async function registerForPushNotificationsAsync() {
  ...
  return token;
}

Thank you.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Try moving the async function outside of the useEffect function:

const someAsyncFunc = async () => {
    console.log("Before await")
    try {
        const token = await registerForPushNotificationsAsync();
        await setDoc(doc(db, "devices", token), { test: "test" })
        console.log('Success')
    } catch (error) {
        /// do error handling
        console.log(error);
    }
    console.log("After await")
}

useEffect(() => {
  someAsyncFunc();
}, []);
about 4 years ago · Santiago Trujillo Denunciar

0

If there a reason why you want to use await there ?

Otherwise you should try to do this using only .then and syncronious code :

useEffect(() => {

    return registerForPushNotificationsAsync().then(async token => {
    
    // The following gets called
    console.log("Before await")

    // The following does not complete when I first launch the app.
    return setDoc(doc(db, "devices", token), { test: "test" })
        .then(x => {
        // The following does not get called
        console.log('Sucess')
        
        })
        .then(() => {
            // The following does not get called
            console.log("After await")
            return () => {};
        })
        .catch(error => {
        // The following does not get called
        console.log('Error')
        })
    });
}, []);
about 4 years ago · Santiago Trujillo Denunciar

0

use async await as follows.

useEffect(() => {
  registerForPushNotificationsAsync().then(async (token) => {
    // The following gets called
    console.log('Before await');

    try {
      await setDoc(doc(db, 'devices', token), { test: 'test' });
      console.log('Sucess');
    } catch (error) {
      console.log('Error');
    }

    // The following does not get called
    console.log('After await');
  });

  return () => {};
}, []);
about 4 years ago · Santiago Trujillo 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