Skip to main content
added some description to answer
Source Link
Jimmy
  • 9.1k
  • 1
  • 32
  • 44

Maybe I'm oversimplifying your question, but this is what async/await are good for.

For sequences of actions that you need to happen one after another, you call them sequentially with await:

await doOneThing();
await doNextThing();
await doFinalThing();

And this entire sequence itself can be an async function that can either be awaited on or launched asynchronously.

Larger Example Code:

// example actions that are slow
async function doA() { await delay(100); }
async function doB() { await delay(1000); }

// awaiting this function returns in approximately 0 milliseconds
async function do_A_and_B_and_return_immediately() { 
    doA();
    doB();
}

// awaiting this function returns in approximately 1100 milliseconds
async function do_A_afterwards_do_B_afterwards_return() { 
    await doA();
    await doB();
}

async function main() { 
    // this is quick
    await do_A_and_B_and_return_immediately();
    // this is slow
    await do_A_afterwards_do_B_afterwards_return();
    // this seems like what you're asking for - 
    // do a sequential task but don't wait up for it.
    do_A_afterwards_do_B_afterwards_return()
}

Maybe I'm oversimplifying your question, but this is what async/await are good for.

// example actions that are slow
async function doA() { await delay(100); }
async function doB() { await delay(1000); }

// awaiting this function returns in approximately 0 milliseconds
async function do_A_and_B_and_return_immediately() { 
    doA();
    doB();
}

// awaiting this function returns in approximately 1100 milliseconds
async function do_A_afterwards_do_B_afterwards_return() { 
    await doA();
    await doB();
}

async function main() { 
    // this is quick
    await do_A_and_B_and_return_immediately();
    // this is slow
    await do_A_afterwards_do_B_afterwards_return();
    // this seems like what you're asking for - 
    // do a sequential task but don't wait up for it.
    do_A_afterwards_do_B_afterwards_return()
}

Maybe I'm oversimplifying your question, but this is what async/await are good for.

For sequences of actions that you need to happen one after another, you call them sequentially with await:

await doOneThing();
await doNextThing();
await doFinalThing();

And this entire sequence itself can be an async function that can either be awaited on or launched asynchronously.

Larger Example Code:

// example actions that are slow
async function doA() { await delay(100); }
async function doB() { await delay(1000); }

// awaiting this function returns in approximately 0 milliseconds
async function do_A_and_B_and_return_immediately() { 
    doA();
    doB();
}

// awaiting this function returns in approximately 1100 milliseconds
async function do_A_afterwards_do_B_afterwards_return() { 
    await doA();
    await doB();
}

async function main() { 
    // this is quick
    await do_A_and_B_and_return_immediately();
    // this is slow
    await do_A_afterwards_do_B_afterwards_return();
    // this seems like what you're asking for - 
    // do a sequential task but don't wait up for it.
    do_A_afterwards_do_B_afterwards_return()
}
Source Link
Jimmy
  • 9.1k
  • 1
  • 32
  • 44

Maybe I'm oversimplifying your question, but this is what async/await are good for.

// example actions that are slow
async function doA() { await delay(100); }
async function doB() { await delay(1000); }

// awaiting this function returns in approximately 0 milliseconds
async function do_A_and_B_and_return_immediately() { 
    doA();
    doB();
}

// awaiting this function returns in approximately 1100 milliseconds
async function do_A_afterwards_do_B_afterwards_return() { 
    await doA();
    await doB();
}

async function main() { 
    // this is quick
    await do_A_and_B_and_return_immediately();
    // this is slow
    await do_A_afterwards_do_B_afterwards_return();
    // this seems like what you're asking for - 
    // do a sequential task but don't wait up for it.
    do_A_afterwards_do_B_afterwards_return()
}