I am wondering if there exists a way to half way synchronize javascript functions, where data is requested on the fly. I don't want to make it purely blocking (which I guess is impossible), I just want a nice way to write the overall algorithm in one piece.
Let's take the following example:
function(){
command1;
var x1 = something;
async_function(x1, callback1);
}
function callback1(data){
var x2 = command2(data);
async_function2(x2, callback2);
}
function callback2(data1){
command3(data1);
}
I would like to write something like:
function(){
command1;
var x1 = something;
call(async_function, [x1], callback1);
var x2 = command2(data);
call(async_function2, [x2], callback2);
command3(data1);
}
- I tried playing around with the
caller
property of functions, but I'm not familiar enough with the execution environment. - I also tried writing a function "call" like above, using the
apply
function to pass in a custom callback which returns the result of the async function to it's caller.
What bugs me is that while programming\debugging, I have to follow the code from one function into another into another (just like the movie inception). I want one place to write the high-level algorithm without decomposing it into many functions.
For example, If I want to write A* algorithm, but the "getNeighbors" function is asynchronous.