How can i make function in a loop to wait for others to complete. Please help

my functions within a loop seem to behave strangely, I would appreciate if anyone could direct me on how to synchronize my code so that it as desired.

function run()
{
   for(val x  = 0; x < 10; x++){
    other_processing(x);//loads some stuff 
    other_simple_fn();///requires "other_processing to fcomplete first"  but seems to complete before 
                     /// "other_processing()"
   }
}

how can I force other_simple_fn() to wait till other_processing() completes its processing.

Thanks.

Use

as callback function to

.

Thank you let me look that up.

I found a solution, it involves promises in JS as:

function wait_promise(ms) 
	{
		return new Promise((resolve, reject) => {
		setTimeout(() => {
		resolve(ms);
		}, ms )
		})
	}  


async function run()
{
   for(val x  = 0; x < 10; x++){
    other_processing(x);//loads some stuff 
    wait wait_promise(6000); //wait for 6 seconds or any resonable ms for other_processing to complete 
    other_simple_fn();///this now behaves perfectly
   }
}

am not sure if that is efficient but it works like charm.

for All who helped, Thank you.

Does this asynchronous function other_processing return a Promise or have an optional callback parameter? I assume you’re using some library, so hopefully there’s public documentation.

I’m asking because the “wait 6 seconds” solution is not a solution to your original problem of:

how can I force other_simple_fn() to wait till other_processing() completes its processing

Your solution is inefficient when the asynchronous function takes fewer than 6 seconds to complete, and fails when it takes longer than 6 seconds.

There are solutions that fix your original problem, but the answers change depending on if the asynchronous function is something you wrote, or if it’s code from a library.

1 Like

This is like my second JS code, other_processing() doesn’t return any promise nor is it aware of the existence of any promise whatsoever, it just seems to be the sanest thing I could do to wait for it to finish… it seems to be processing for a max of 2-4 seconds, so my other_simple_stuff() will; have to hang on there while waiting for the empty promise.

The promise does nothing important just placing a delay … I, don’t even fully understand it but it works, I tried using recursion but the way I was using it led to undesired side effect and I avoided that route.

“It’s inefficient” I can smell that but I have no idea what I would do to resolve that, if you got new ideas I would appreciate.

I’m asking because the “wait 6 seconds” solution is not a solution to your original problem
It is the only solution that seemed to produce some behavior close to what I wanted, recursion was not even close, this was magical, i spent the whole day searching.

Try having a look through this module on MDN about asynchronous JavaScript.

Basically you have two options, rewrite other_processing so that it does one of the following:

  • Accepts a callback function as a parameter that will be called after it has finished asynchronous processing.
function asyncFunc(callback) {
  /* Do some asynchronous stuff  */
  /* ........................... */
  /* Once finished call callback */
  return callback();
}

function syncFunc() {
  /* Do some synchronous stuff */
}

function main() {
  asyncFunc(syncFunc);
}
  • Returns a Promise that resolves once it has finished asynchronous processing.
function asyncFunc() {
  return new Promise((resolve, reject) => {
    /* Do some asynchronous stuff  */
    /* ........................... */
    /* Once finished call resolve*/
    return resolve();
  }
}

function syncFunc() {
  /* Do some synchronous stuff */
}

/* Using then method */
function mainWithThen() {
  asyncFunc().then(syncFunc);
}

/* Using async/await */
async function mainWithAwait() {
  await asyncFunc();
  syncFunc();
}

Thank you very much, wonderful, I’ll analyse that… see if there is some to solve the problem…

This weird behavior for other_processing() basically occurs like this:

function other_processing()
{
     ///code accessing some other links that might take up to several seconds 
   ///above is where the problem is the code is executed but this function doesn't wait for the result
   ///so I get undefined result or an exemption 

   //some other code
}

see, I hope I’ll follow your approach to solve it.

am glad.