Async/Await usage vs Promises

Difference between Async/Await and Promises

Async Await functions works similar to Promises, with minor differences.

When you use a Promise, the single thread asynchronously executes your async operation and then continues and executes the next line of code.

Whereas in Async/Await, it will execute all of the Await first, before moving on.

Example – Async/Await vs Async/Promise

We have a getWebcast function that is going to wait 2 seconds of wait time and then returns data. This simulates a fetch operation that goes to a server, retrieves some data and returns it.

The execution of a Promise is always asynchronous.

The result is:

— F —
— getWebcast —
PromiseĀ {}
— result of fetch in F —
using Promises — end of F —
end of script…

You see that the function execute. The returned Promise returned by F() is a pending Promise and no one is using it. It executes line by line down the list.

In order to solve this asynchronous issue, we use .then on the returned Promise object.

So now, you’ll see that the single thread will execute getWebcast’s returned Promise a bit,
then continue execution and log. Goes back and process Promise a bit, then keep going on to the next execution.
Eventually, when the Promise in F is done, the thread goes into the then and then logs.

Hence, note that the execution continues right after the start execution of our Promise.

results:
— F —
— getWebcast —
using Promises — end of F —
end of script…

wait 2 seconds

— result of fetch in F —
{data: “payload”}

Using async/await

Now that we’ve seen how Promises work, let’s look at async await.

await is only valid in async function

In order to use await, you must wrap it inside of an async function. You can also use Promises in async functions.
However, there is a minor difference. With async/await, the execution will stop at ‘await’ within the async function.
Execution will continue in the async If and Only If the await has been executed.

However, execution OUTSIDE of the async function will be asynchronously processed. Let’s take a look at an example.


— F —
getWebcast —
end of script…

wait 2 seconds

result of fetch in F —
{data: “payload”}
using Await — end of F —

So as you can see in the async function F, the execution actually stops at getWebcasts()
It does not go onto the three logs.

But when function F does execute it, the thread will asynchronously execute the log ‘end of script…’ after F().
This is obvious in the output as it is printed to the console, and then we simulate the 2 second wait.

After the 2 second wait, we then see the rest of the code (AFTER await in the async function) get executed.

If you want everything to run top down, without the thread running down the execution line on printing “end of script…” when its waiting for the 2 seconds…then simply put await in front of F:

This ensures that we will always wait until an async operation is done before we move on.

This applies also to when we use then also.


output:
— F —
— getWebcast —
end of script…

wait 2 seconds

result of fetch in functionalityOne —
{data: “payload”}
using Await — end of F —

In an Async function, execution must finish on Await, before it moves on to the rest of the async function code. However, it will continue asynchronous code outside of the Async function.

…in other words, await BLOCKS the code execution WITHIN the async function.

Additional Example

If you do not return a Promise, async function will return a Resolved Promise

As for async operations, it won’t work here because the Promise here is resolved already.
If you want to do a future resolve, make sure to create your own Promise, and resolve where you want.