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

288
Views
NodeJS setImmediate and private class method

So, first time experimenting with setImmediate. It seems it can't run private class methods. Is that so? If yes, can someone explain to me why?

Doesn't work

Private class method as argument to setImmediate. Throws Callback must be a function. Received undefined

class TestPrivate {
    public start(): void {
        setImmediate(this.looper);
    }

    async looper(): Promise<void> {
        console.log(`${new Date().toLocaleString()}`);
        await sleep(500);
        setImmediate(this.looper);
    }
}

const testPrivate: TestPrivate = new TestPrivate();
testPrivate.start();

This does work

Static class method as argument to setImmediate

class TestStatic {
    public start(): void {
        setImmediate(TestStatic.looper);
    }

    static async looper(): Promise<void> {
        console.log(`${new Date().toLocaleString()}`);
        await sleep(500);
        setImmediate(TestStatic.looper);
    }
}

const testStatic: TestStatic = new TestStatic();
testStatic.start();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you pass this.looper, then the by the time the function is called, looper will no longer have a correct reference to this, so this breaks on the second iteration.

Solve this by using one of the following:

setImmediate(this.looper.bind(this));
setImmediate(() => this.looper());
about 4 years ago · Juan Pablo Isaza Report
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!