This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Then, I tried to use setTimeout as below. I do not want the below function getValueByContent return any value unless it gets the right value when testGetValue is ready.But it seems the setTimeout() launches a separate thread and do not make the main function wait or sleep at all. As a result, the function getValueByContent() will return immediately when it is being executed at the first time regardless the testGetValue is ready or not.
the simpliest way for you would be to break original function with a wait operation in the middle into 2 functions. I mean if you have this a function looking like that:
than you should get something like:
than your "start wait process" function will look like
To make this function better, instead of hardcoding call to x2, you can pass it as a parameter.
The problem with this code though would be transfer of variables from x1 to x2. You have couple choices there, for example, you can have all variables defined in x1 and needed in x2: 1. declared as global 2. have an object declared as global, that contains all the variables. 3. have an object, that contains both functions and a variables. but that would be up to you.