Wednesday, 12 April 2017

How to execute synchronously using setInterval or setTimeOut?

I have a function as below:

function foo(args1, args2, retry)
{
    if (retry <= 0)
        return false;

    var isDone = callAnotherFunction(args1, args2);

    if(!isDone) {
       setInterval(function () {
           foo(args1, args2, retry-1);
       },
       2000);
    }
    else
      return true;
}

So I am not sure if the above implementation is correct. But I need to use this function in another function. And use the above function in an if block to decide if the other statement needs to be executed. Below is the usage of the above function.

function useIt(args1, args2)
{
    // Other code
    let store = function() {
       if(!foo(args1, args2, 5)) {
           cleanStorage(args1, args2);
           return;
       }
}

So the problem is in function useIt(), cleanStorage() does not wait for foo() to be executed if I am using setInterval or setTimeOut. So how do I need to implement the function foo() ? Kindly help me.



via Suhas Kulkarni

No comments:

Post a Comment