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

417
Visualizações
NodeJS: How to kill an active instance of a class

I've been searching for an answer for quite a while and the best I've done so far is highly inefficient and I am most certainly sure that there's a simpler solution to this.

I currently have a class that is trying to do a certain task to retrieve an output, to await for that output I use Promise resolve/reject.

class Example {
    start() {
         return new Promise(async (resolve, reject)) {
              var step_1 = await this.function1();
              var step_2 = await this.function2(step_2);
              var step_3 = await this.function3(step_3);
         
              resolve(await this.function4(step_3);
         }
    }
}

module.exports = Example;

Let's pretend function1-4 exists and does a certain task that takes a while to do. Then we create multiple of these instances:

const Example = require("./Example.js");
for (let i = 0; i < 10; i++) {
     const example = new Example();
     example.start();
}

So this takes seconds to minutes to return an output, but my problem currently is how to kill all of them mid solving.

My first approach was to just reject or resolve an output in an setInterval that checks if the code has been killed or not, but that doesn't seem to work because of 1 of 2 problems:

  • If the process moves on to a next function, it'll still do that function.
  • If one of the step functions is currently active, and it finishes right after you resolve/reject the start() method, it'll just keep continuing down the code.
const { isKilling } = require("./main.js"); // isKilling() returns either true or false

class Example {
    start() {
         return new Promise(async (resolve, reject)) {
             
              var interval = setInterval(function() { 
                   if (isKilling()) return resolve("KILLED"); // or reject("KILLED");
                   // The instance of this class still continues.
              }, 1);
 
              var step_1 = await this.function1();
              var step_2 = await this.function2(step_2);
              var step_3 = await this.function3(step_3);
         
              resolve(await this.function4(step_3);
         }
    }
}

module.exports = Example;

For how I solved it very inefficiently, I did the setInterval thing but then add a piece of code that checks if the class has been killed and it just returns on nearly every single line (I know, it's bad)

So I am hoping if anyone here can help me out! Thank you in advance!

about 4 years ago · Juan Pablo Isaza
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