The following JS code:
function promise_GetSomething() {
return new Promise(resolve => {
setTimeout(function () {
resolve("Something gotten after 3 sec.");
}, 3000);
});
}
async function logAboutSomethingGotten() {
console.log("Before getting 3-sec-something.");
var somethingGotten = await promise_GetSomething();
console.log(somethingGotten);
console.log("After getting 3-sec-something.");
}
function doSomething() {
console.log("START");
logAboutSomethingGotten();
console.log("STOP");
}
doSomething();
...prints the following:
START
Before getting 3-sec-something.
STOP
Something gotten after 3 sec.
After getting 3-sec-something.
How can I adjust it to print the following?
START
Before getting 3-sec-something.
Something gotten after 3 sec.
After getting 3-sec-something.
STOP
Also, if the async logAboutSomethingGotten() returns a value, how can the value be gotten and used synchronously?
EDIT (UTC 2019-01-12 11:14 PM):
Thanks to the current answerers. The current answers all mostly suggest making doSomething() async and await logAboutSomethingGotten(). I had known this too, but because it could lead to maybe an endless async-await code, I didn't like that solution. Also, the option involving piping a .then(function() { console.log("STOP"); }) unto logAboutSomethingGotten() too would easily somehow solve that but not in all scenarios, like for the synchronous part of my question.
So, I'm particularly interested in if logAboutSomethingGotten() returns a value (e.g. after making an Ajax call, because synchronous Ajax is being deprecated). By what clever means can I use this returned value in a synchronous flow?