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

107
Visualizações
Continue function if returned function is not returning

I am trying to reduce cyclomatic complexity but this is a general query. if function b is not returning anything function a should continue to return 3

function a() {
  return b();
  return 3
}

function b() {
  if (false)
    return 2
}
alert(a())

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

More manually you could store the result of b() in a variable, check the variable, and conditionally return or continue. Though in this simplified version you could also just rely on the Nullish coalescing operator when returning.

For example:

function a() {
  return b() ?? 3;
}

function b() {
  if (false)
    return 2
}
alert(a())

Essentially the expression resolves to either the result of b() or, if that result is null or undefined, resolves to 3. Then the result of that overall expression is returned.

about 4 years ago · Juan Pablo Isaza Relatório

0

That works with my test, "I used console.log not alert"

function a() {
  let val = b()
  return val == null ? 3 : val;
}

function b() {
  if (false) return 2;
  return null; // If anytings doesnt return that function we returns null 
}
alert(a());

Performance test for nullish operator or standart one line if https://jsbench.me/ool12lcckh

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