I've seen similar questions asked regarding this problem. I'm pretty new to javascript and I couldn't figure it out.
I have a function which calls another function.
sayHello() has an async call.
var hello_message = null;
function invokeSayHello(msg) {
sayHello(msg);
//next action
return hello_message;
}
function sayHello(msg) {
// simulate async call
setTimeout(function(){hello_message = msg + " World";},1000);
}
In this case hello_message is returned as null. How would I wait for that async call to complete before the next action line executes in invokeSayHello() function so the returned hello_message would not be null.
I think I'm supposed to use a callback but not sure how to do it.. Also, I call invokeSayHello() from a java file using executeScript()/Selenium
Appreciate all the help.