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

137
Vistas
Return boolian in parent function after child promise resolves

I am working in a framework that requires the parent function to return true or false. Within that function, I'd like to run an async operation.

How do I make the parent function return true or false after an async action?

Example of what doesn't work:

   function parent() {
     const child = new Promise();
     child.then(
       function (success) { return true },
     )
   }

The problem is that returning is happining within the context of the child.then() function.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This necessarily requires that the parent function be async as well, unless you want to block the main thread, which would be considered an anti-pattern.

Do this:

const parent = async () => {
    const childResponse = await child();
    if (childResponse === "something") {
        return true;
    } else {
        return false;
    }
};

Do NOT do this:

const parent = () => {
    let promiseResult, parentReturnValue;

    child.then(result => {
        promiseResult = result;
        if (promiseResult === "something") {
            parentReturnValue = true;
        } else {
            parentReturnValue = false;
        }
    });

    while (promiseResult === undefined) {
        // block the main thread
    }
    
    return parentReturnValue;
};
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