I want to call the three functions someTask1 , someTask2 and someTask3 in this order. However, the someTask2 function includes an Ajax call and continues to call it recursively with setTimeout if the required value is not returned. The code is as follows:
doListOfTasks: function(myparam){ var someObj = someTask1(myParam); someTask2(someObj); someTask3(someObj); }, someTask2: function(someObj){ $.ajax({ url: "someUrl.do", type: "POST", data: ({"id": rowObject.instanceId}), dataType: "json", async:false, success: function(res){ if(res.prop1 != 'desired'){ setTimeout(function(){someTask2(someObj);}, 2000); } } } ); },
As you might have guessed, the execution of this code does not someTask2 for someTask2 to return before calling someTask3 .
I want the code inside doListOfTasks executed sequentially. How can i do this?
Also, I don't want to use someTask3 code in success for hard code. For instance. I do not want to do this:
success: function(res){ if(res.prop1 != 'desired'){ setTimeout(function(){someTask2(someObj);}, 2000); }else{ someTask3(someObj); } }
How can this be achieved?
thanks
Edit # 1
The problem is not the ability to call functions ... but the problem is synchronization. I want someTask2 to complete everything it does, and only then someTask3 is called.
someTask2 causes reuse of setTimeout ... I assume this causes a new thread, and someTask2 returns after the first call ... of someTask3 trigger in the main thread. However, a separate thread generates (and kills) in each setTimeout call until the required criteria are met.
That is why, while someTask2 is still cyclical, a call to someTask3 is triggered.
I donβt know how right I am.