1

I get an interview question. I abstract it as below: If you call setInterval, and the callback (or parameter) function is blocked, what should you do?

setInterval( () => {
  someFunction(); // ? What should I do if someFunction is blocked
}, 100);

The original interview question has two sections.

  1. The first one is, that you simulate setInterval with setTimeout.
  2. The second one is, that if callback (or parameter / ? the term I used is right) function is blocked, how do you do with the problem?

The first one, there is my solution:

function setInterval(func, period){
  setTimeout( () => {
    func();  // what if func is blocked, how do you deal with it?
    setInterval(func, period);
  }, period);
}
setInterval( () => {
  console.log("There is simulated setInterval ... ");
}, 100);

To the second question, How do I solve it?

Here is: Js Bin

soarinblue
  • 1,517
  • 3
  • 21
  • 30
  • 4
    What do you mean by blocked? – ibrahim mahrir Feb 12 '17 at 03:39
  • I/O blocked, network blocked, or something error happened. @ibrahimmahrir – soarinblue Feb 12 '17 at 03:45
  • you cannot answer that in general. It depends on what exactly is blocked there, and why you're trying to access it. It's completely up to you how you deal with errors, wether you can use a fallback or mock, wether you're letting this thing fail silently or wether you tell the user that something went wrong or that some service or functionality is unavailable. – Thomas Feb 12 '17 at 04:01
  • `(typeof someFunction === 'function') ? someFunction() : false;` – daniloprates Feb 12 '17 at 04:01

0 Answers0