I know this question has been asked many times in many different ways, but I am still having trouble identifying a good solution to the following problem.
How can I wait for the callback of this asynchronously-executed inner function to complete prior to returning from the outer function?
function outer() {
var result = false;
var innerAsynch = function() {
result = true;
}
setTimeout(innerAsynch, 1000);
waitForInnerAsynch(); //blocking
return result; //true
}
1) I am well aware that this is bad practice for 99.999% of use cases, so please don't tell me that it shouldn't be done!
2) Please feel free to completely restructure this code... my only requirement is that outer() returns something which blocks the browser until innerAsynch() is done and passes the following test:
if(outer()) {
//1 second later.... yippee!
}
Also I am using jQuery, but I would prefer not to use a plugin unless it really makes sense to do so. Thanks!
Update
I want to reiterate that my goal is to fully encapsulate the asynchronous execution within the synchronous call to outer() without arguments.
In other words, this should work:
<a href="something" onclick="return outer()">A very slow link</a>
Perhaps this is not actually possible, in which case I am fine using callbacks, but I want to develop a better understanding of why that is the case.