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

270
Views
How can I wait for a function to be called by an unknown caller?

I have a callback function that gets called by some other object which I can't control. I need to wait until this callback function is called, I don't care by who.

var successFunc = function() {
    // do stuff
}

myObject.onSuccess = successFunc;

// hang on until successFunc is called...

I found this hacky workaround, but it sucks:

var completed = false;

var successFunc = () => {
    // do stuff
    completed = true;
}

myObject.onSuccess = successFunc;

while (!completed) {
    sleep(200); // sleeps for 200 ms
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Create a promise, pass its resolve function as callback to the unknown caller somehow, listen to the promise's resolution:

let callback;
const promise = new Promise(r => callback = r);

promise.then(() => console.log('unknown caller called'));

setTimeout(function unknownCaller() {
    console.log('randomly calling back');
    callback();
}, Math.random() * 5000);

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!