New js user: I'm trying to use async with .then to make sure an ajax script has time to complete before I try to access its result. My ultimate goal here is a crude unit-test, I want to make sure an ajax function completed its task. I can't seem to get the code to wait properly. This example strips away as much of the complication as I can and attempts to leave the bare-bones problem:
async function tempfun2() {
//in the "real" function, this is where time-consuming work communicating with server is done
return 1;
}
function tempfun1() {
tempfun2().then(
function(value) {
console.log("value in then = " + value);
if (0 == 1) //in the "real" function, this is where I'd check to see if the work was done
{
return 0;
} else {
return 1;
}
)
}
let q = tempfun1();
console.log("value_after_fun = " + q);
when I execute the code, the "value after fun" alert comes before the one within the .then statement. In a sense, I think this is working as designed, because I'm clearly escaping tempfun1() while tempfun2() is still working. However, this isn't what is desired, I need to make sure tempfun2() finishes so I can check that the work was complete AFTER it finishes.
I tried using several combinations of .then as well as using async/promises and had the same problem. I can't seem to wait for tempfun2() to finish before I escape tempfun1().
I've reviewed several similar help topics here, and they all seem to agree that promises or .then is the way to go, but I'm clearly missing something very basic here.