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

142
Visualizações
javascript make child function stop parent function

Hello I am currently executing a child function from a parent However if something particular happens I want the child function to stop the rest of the parent function to stop executing

How do I do this without using a return ?

The child function is a module

Main.js

console.log("Beginning") 
const func_import = require("/module.js")
func_import()
console.log("not to be executed in certain cases")

module.js

module.exports = async() => {
   console.log("something")
}

I tried to use a simple return value However this just returned an undefined even after I used await

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

0

It's possible, but very, very strange. You can throw an error in the child:

module.exports = () => {
    if (condition) {
        throw new Error();
    }
}

This will cause the parent (and each of its parents) to stop executing at the point of the function:

console.log("Beginning") 
const func_import = require("/module.js")
func_import() // <-- stops here
console.log("not to be executed in certain cases")

Don't do this. Errors should be used for exceptional circumstances (there's a reason they're called "exceptions"), not for simple control flow. Return a value instead.

module.exports = () => {
    // other code
    return condition;
}
console.log("Beginning") 
const func_import = require("/module.js")
const condition = func_import();
if (!condition) {
    console.log("not to be executed in certain cases")
}
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