Exit from function from inside Timeout

The problem is that I have a setTimeout function inside another one:

function checkBrowser() {
  setTimeout(function() {
    if(some.condition) //exit function checkBrowser() here
  },5000);
  setTimeout(/*something that mustn't run if some.condition is true*/,5000);

}

I need to exit the first function from inside setTimeout. Help.

Upvotes: 1

Views: 1836

Answers (3)

Redu
Redu

Reputation: 26191

You are running your conditionals in the asynchronous / retarded timeline and once you enter into that timeline there is no way out. You must remain there all the way your logic goes including any recursive activity. However i would advise you to use promises and move all logic to the then part for sake of functional programming.

Let's first take our functions one by one and see how all gets geared up by the promises.

OK lets assume that we have a generic async function called waitAndDoStg which takes a data and a callback as two of it's arguments. Let's assume our callback is of error first type.

var waitAndDoStg = (data,cb) => setTimeout(_=> cb(false, data.value),data.duration),

It will never return any errors. So after data.duration milliseconds pass it will invoke our callback like cb(false, data.value) and will pass the data it took from us back to us. A stupid function is what it is.

So ok we need to promisfy this async function. We have a standard promisfy utility function which takes any async function provided and gives us a promise out from it. The callback has to be error first type though.

promisfy = (data,func) => new Promise((v,x) => func(data,(err,res) => err ? x(err) : v(res)));

OK, instead of resolve and reject i use v and x names respectively. Well i guess it is reasonable to use v for resolve since it looks like a check mark and x is fine for reject i suppose.

So here is the rest of the code. It's promises and recursive and everything but i guess functionally this is a very readable code.

var     promisfy = (data,func) => new Promise((v,x) => func(data,(err,res) => err ? x(err) : v(res)));
  decrementByOne = n => --n,
    waitAndDoStg = (data,cb) => setTimeout(_=> cb(false, data.value),data.duration),
countDownPromise = n => {n && console.log(n);
                         promisfy({value:n, duration:1000}, waitAndDoStg)
                         .then(decrementByOne)
                         .then(val => val !== 0 ? countDownPromise(val)
                                                : console.log("Ignition...!"));
                        };
countDownPromise(3);

Upvotes: 0

Thank you very much for helping! I've done it in another way:

function checkBrowser() {
  setTimeout(function() {
    if(some.condition) {
      //something here
    } else {
      setTimeout(/*something that mustn't run if some.condition is true*/,5000);
    }
  },5000);
}

Upvotes: 0

Quentin
Quentin

Reputation: 944171

You can't. The code is asynchronous.

The first function will have finished running and exited before the function you pass to setTimeout has even been called.

Upvotes: 1

Related Questions