Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

410
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!