Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda