Skip to main content

Posts

Showing posts with the label synchronous

JavaScript: asynchronous function that resumes to synchronous

I have a large script that is very synchronous. 1) it runs a function like this: var result = doSomethingSynchronous(myVar); if (result === 'something') { doSomethingElse(); } the doSomethingSynchronous function has a for loop that returns a value on a if statement: var i = 0; le = someVariable.length; for (; i < len; i++) { if (someVariable[i] === 'aString') { return true; } else { doSomethingElse(); } } Because the for loop and the processing can be intense, I've replaced the for loop with an iterative queue processing function that runs itself asynchronously using setTimeout after shifting one element from the array. Once it runs asynchronously of course it doesn't return the value and assigns it to the 'result' variable in the first snippet synchronously, and the if statement there always fails. My question is, is there a way to keep this part synchronous? Have the value be assigned to result before the script goes on to th