How to make JS async function run in sync with others?

Viewed 237

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?

3 Answers

Do this->

async function doSomething() {
    console.log("START");
    await logAboutSomethingGotten();
    console.log("STOP");

}

Check the snip-

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.");
}

async function doSomething() {
    console.log("START");
    await logAboutSomethingGotten();
    console.log("STOP");
    
}

doSomething();

Just mark doSomething as async and then await the logAboutSomethingGotten(); call.

If the async logAboutSomethingGotten() returns a value, how can the value be gotten and used synchronously?

An asynchronous result can never be used synchronously. Of course, after waiting for the promise you can always use the return value as normal.

Set your doSomething function to also be asynchronous, and await logAboutSomethingGotten.

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.");
}

async function doSomething() {
    console.log("START");
    await logAboutSomethingGotten();
    console.log("STOP");
}

doSomething();
Related