Learn HTML (GDSC ENSTA Workshop)
GDSC ENSTA Workshop on HTML for beginners. Learn the basics of HTML and how to create a simple webpage.
November 10th, 2024
By: Ismail M. BOUSSEKINE
Reading time: 4 min
Async/Await is a way to handle asynchronous calls but in a way that will make our code look more synchronous and as a result more readable.
Important to know that:
Aync/Await is not a replacement for Promise, its actually using promise.
async
is used while declaring a function async function getData(){}
async
will convert a function to a Promise
that can be handled using .then()
or .catch()
Since it converts the function to a Promise the resolved value is whatever get returned from the async
function
Only Inside async
function we can use await
await
is used to pause the execution of a function.
We use await
before calling a Promise to pause the rest of code execution until that promise is resolved.
await
works only with Promises, it does not work with callback functions
await
can be used only inside async
functions
Anything that can be done by async/await can be done using only Promises, to show how it works we will have 2 examples where we will see the same result first using Promises, and then using async/await
here is what getAsyncData()
does
async/await solution
Its very important to know the consequences of using await
as it completely pause the function execution, in the next example we will see how using await
incorrectly can result in performance issues.
In this example we will call our getAsyncData()
that simulates the async call 3 times
As we can see from the result the value 3 is returned after 6 seconds because getAsyncData is called 3 times sequentially, num2 will wait 2 seconds until num1 is returned and num3 will wait 4 seconds until num1 and num2 are both returned.
This is a performance issue because num1, num2, and num3 are all not related to each other which means we don't need to wait until num1 or num2 are returned to start execution of num3.
To fix that we can use await only in return statement to indicate that the functions are all going to be executed directly but we need to wait for all results
In this example we will call our getAsyncData()
that simulates the async call 3 times
This is a more optimized code for example 11.2 as the result 3 will be printed after 2 seconds
All the above examples are not utilizing try/catch
blocks which is very important while using await
if a promise is called with await
and the promise got rejected, an exception will be thrown.
Whenever await
is used it's important to use try/catch
to be able to catch errors correctly.
The above unrelated async calls can be acheived easily with Promises using Promise.all([getAsyncData , getAsyncData , getAsyncData])
async
is used to convert a function to a Promise that can be handled using .then
or .catch
, and the resolved value is whatever is returned from the async
function.await
pauses the execution of an async
function until the Promise is resolved, making asynchronous code appear synchronous and easier to manage.try/catch
blocks to handle errors when using await
to catch exceptions thrown by rejected promises.await
when making multiple independent asynchronous calls. Use techniques like Promise.all
to execute them in parallel.By understanding and applying async
/await
correctly, you can write cleaner, more maintainable asynchronous code and avoid common pitfalls related to performance and error handling.
GDSC ENSTA Workshop on HTML for beginners. Learn the basics of HTML and how to create a simple webpage.
November 10th, 2024
Learn CSS with GDSC ENSTA Workshop and get started with web development. This workshop will help you understand the basics of CSS and how to style your web pages.
November 27th, 2024
This article will guide you on how to choose your linux distribution based on your needs and use case, It will also provide a list of the best linux distributions out there.
September 13th, 2024