0

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?

Matthew
  • 1
  • 2
  • Do all first three functions return the same `value`? Seems unlikely (and redundant if true) and makes things confusing. You may want to take some time to [read this](https://stackoverflow.com/help/reprex) for help in creating a clearer MCVE – cybernetic.nomad May 16 '19 at 15:12
  • 1
    If the problem starts after call geDimensions, it's probabily that the problem resides in getDimensions definition. Are you using shared global variables inside those functions? – Miguel May 16 '19 at 18:46
  • @cybernetic.nomad Sorry, I suppose I sacrificed readability for brevity. That's on me. I can edit if that helps. I'll definitely read your link. – Matthew May 29 '19 at 23:06
  • @Miguel, after some thinking it seems that running functions from a loop causes the timing to get off so instead of asynchronous, I'm thinking how to format to run synchronously. Since I'm in InDesign, I can't normally use a promise like I typically would in other JavaScript projects and I was trying to avoid callback hell. – Matthew May 29 '19 at 23:06

1 Answers1

0

You can try to chain your functions:

function pod(value, count) {
    return getDimensions(calculateSomething(doPodStuff(value))) }

And in any case you can use $.sleep(1000) instead of alert("...") to slow your script down.

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23