Key problem is that function in a loop is out of sync. I don't get the correct var myDimensions
unless I include the alert() to slow down the process. I'm not sure if I need to use callbacks. I read about them and find them confusing and I want to avoid callback hell. What's happening is that when running a function in a loop, some of my other function calls are out of sync. I am writing this JavaScript as Extendscript for Adobe InDesign so Extendscript does not include modern JS.
I discovered the timing issue after adding an alert()
in the function to return the value to confirm I was getting the correct values. If I add the alert()
it causes a delay which makes the code run correctly. When I remove alert()
it fails again. I've tried to simplify my code for brevity below instead of pasting the full 1,500 line file.
function calculateSomething(podValue){
return podValue + 1;
}
function doPodStuff(value){
return value;
}
function getDimensions(calculation){
return calculation * 2;
}
function pod(value, count){
var podStuff;
var calculation;
var myDimensions;
podStuff = doPodStuff(value);
calculation = calculateSomething(podStuff):
myDimensions = getDimensions(calculation);
/* this is where my timing is off and podStuff and calculation get out of sync,
I determined this after placing an alert(myDimensions) right here If I add the alert(),
the delay makes it work, when I remove alert() it fails again.
*/
return myDimensions;
}
function row(value, count){
var myPod;
for(var i = 0; i < count; i++){
myPod = pod(value, 2);
}
return myPod;
}
row('foobar', 4);
My goal is to have the pod()
function run in the for()
loop but not out of order. How do I synchronize or make sure the timing is correct?