I've got some code which resembles this basic structure:
Assuming the hypothetical "async" function is completely unmodifiable library code, what can I do in "myFunc" to ensure that "a" gets properly set to "step 2" before the function returns?
Code:
var a,
async = function(f) {
setTimeout(function() {
var r = 'step 2';
f(r);
}, 1);
},
myFunc = function() {
a = 'step 1';
async(function(r) {
a = r; // should reset "a" to "step 2" but doesn't finish in time
});
return a; // always returns step 1
};
// always returns step 1
console.log(myFunc());
// returns step 2 now that async() is done executing
setTimeout(function() {
console.log(a)
}, 2);
Assuming the hypothetical "async" function is completely unmodifiable library code, what can I do in "myFunc" to ensure that "a" gets properly set to "step 2" before the function returns?