Need help with understanding the code execution flow.
On calling the sayHi() method below, the onSuccess handler is executed first and then the return statement gets executed.
I had the understanding that the function will return immediately and the onSuccess handler will get executed after that.
public String sayHi() {
sayHello().onSuccess(s -> {
System.out.println(s);
});
return "hi";
}
public Future<String> sayHello() {
String s1 = "hello";
return Future.succeededFuture(s1);
}
EDIT: I'm trying to draw a comparison it with JavaScript Promises, where the function passed to the then method always gets executed after the rest of the statements are executed. Like calling the test method below will print 'hi','bye' and then 'Success'
var promise2 = new Promise((resolve, reject) => {
resolve("Success!")
});
function test() {
console.log("hi");
promise2.then((value) => {
console.log(value);
});
console.log("bye");
}