-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSummery EP 23.txt
17 lines (11 loc) · 1.61 KB
/
Summery EP 23.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
• Async/await used for handling promises
• Async always return a promise
° Await can only used inside an async function
° Can only write await keyword infront of a promise
° While awaiting JS Engine does not actually wait rather the function is suspended and call stack is free for other stuffs but it looks like program is waiting at that point
° Use try catch for Error handling and can also use . Catch() method
Promise: Think of a promise as a guarantee made by someone (like a function) to do something and provide you with the result later. It's like ordering food at a restaurant. You get a promise (receipt) saying your order will be ready soon. You can wait for it (.then()) or check on it later (.catch()).
Async/Await: Async/await is like asking someone (a function) to do something for you, but instead of waiting for them to finish right there, you tell them you'll do something else while they work. It's like asking a friend to pick up your order from the restaurant. You can go do other things (like order a drink) while your friend (the async function) waits for the food (awaits the promise).
Ques). for those who thinks if code reaches await of p1 suspends function execution and after 5 sec it should go to await p2 the timer for p2 should start and should take 10 sec there so overall function execution should be 15sec?
Ans). the timer ticking doesn't start at await, instead it started in the beginning of the code only where promise was declared.
If the declaration would have been like inside async p1 declare then p1 await after that p2 declared follwed by p2 await then function would have taken 15 secs