pls expalin me Async await JavaScript
Loading
pls expalin me Async await JavaScript
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Abhishek YadavPosted Mar 14, 2024, 5:43 AM
Async await is a feature in JavaScript that allows you to work with asynchronous code in a more synchronous way. When you mark a function with the
asynckeyword, it automatically returns a Promise. Inside an async function, you can use theawaitkeyword to wait for a Promise to resolve before continuing with the code execution.Here's an example of how async await can be used:
Md SarfarajPosted Mar 17, 2024, 2:29 PM
Please check out the video below. He explained everything very well.
Link: https://www.youtube.com/watch?v=8aGhZQkoFbQ
Vishal YelvePosted Mar 15, 2024, 8:47 AM
Hi Gomij,
Do refer below links, which will help u to understand with examples
https://www.freecodecamp.org/news/javascript-async-await/
Naimish MakwanaPosted Mar 15, 2024, 5:04 AM
The
asyncandawaitkeywords are special syntax in JavaScript that are used to work with promises in a more comfortable and readable way.Async Function: An
asyncfunction is a function that implicitly returns a promise. Theasynckeyword before a function means that the function will always return a promise. Any value returned by the function is wrapped in a resolved promise automatically. Here’s an example:In the above example, the function
f()returns a resolved promise with the result of1.Await Keyword: The
awaitkeyword is used inside anasyncfunction to pause its execution and wait for a promise to resolve or reject before continuing. Here’s an example:In the above example, the function execution pauses at
let result = await promise;and resumes when the promise settles, withresultbecoming its result. So, the code shows “done!” after one second12.Remember, the
awaitkeyword can only be used inside anasyncfunction3. If we try to useawaitin a non-async function, there would be a syntax error1.This
async/awaitsyntax provides a way of maintaining cleaner and more readable code when working with asynchronous operations12.Thanks
Tuhin PaulPosted Mar 14, 2024, 8:18 PM
awaitkeyword can only be used inside an async function.awaitkeyword then returns the resolved value of the Promise.Tuhin PaulPosted Mar 14, 2024, 8:16 PM
asynckeyword is used to declare asynchronous functions.Tuhin PaulPosted Mar 14, 2024, 8:15 PM