My input is javascript code that I can pre-process..
at some specific point in the middle of some function there is a token that I need to replace with some async request(e.g. async AJAX request).
On runtime: When the async request is back (the callback is being executed) I need to return to exactly the same place(with the same context) in the code where I were before executing the async call.
Is it even possible? any ideas?
note that (1) I can pre process and change the code however I had like..
(2) the token can be practically anywhere, in a loop or anywhere else.. (3) the token just represents the location of the the async call, I can parse the entire document
This code might run on a browser or via node.js
Sample code:
function input(num){
num +=10;
<token>
return num;
}
Would translate to some thing like
function input(num){
num +=10;
wait(async-request(...,callback));
return num;
}
Update: After I read your answer here is an additional question,
Javascript: sync to async converter libs
There are several projects that attempt to rewrite code written in a synchronous pattern into async code using continuations. E.g. you might want to take a look at:
https://github.com/Sage/streamlinejs
Some patterns (loops, try/catches) likely present more challenges than simple linear flow, but I’m pretty sure it’s a solvable problem in the general case. I don’t think you’ll be able to do it with simple token replacement, though — you’ll need to parse the javascript and transform it.