You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 18-Async-Await-JS-Magic/README.md
+34-5Lines changed: 34 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ Async functions depend extremely heavily on promises. Async functions are just a
14
14
## 2. The Async Keyword
15
15
16
16
#### The async keyword
17
+
17
18
- Async functions always return a promise
18
19
- If the function returns a value, the promise will be resolved with that value
19
20
- If the function throws an exception, the promise will be rejected
@@ -30,17 +31,18 @@ async function uhOh() {
30
31
uhOh()
31
32
// Promise {<rejected>: Error: oh no!}
32
33
```
34
+
33
35
So we use async in front of a function declaration or function expression, we use it to designate a certain function as an async function.
34
36
35
-
The key thing to understand about this keyword is when you put it in front of a function, this function behaves differently. **It's now going to return a promise**
37
+
The key thing to understand about this keyword is when you put it in front of a function, this function behaves differently. **It's now going to return a promise**.
36
38
37
39
So even though i never write a new promise, i don't explicitly say, hey, return a promise. I'm simply saying return a string because i put async there the function now returns a promise.
38
40
39
41
#### How do we return a promise that is not resolved?
40
42
41
-
- If we want to return a rejected promise, all that we do is
42
-
raise an exeption. So if we throw an exception, we throw an error,
43
-
that promise will be rejected
43
+
- If we want to return a rejected promise, all that we do is
44
+
raise an exeption. So if we throw an exception, we throw an error,
45
+
that promise will be rejected
44
46
45
47
## 3. The Await Keyword
46
48
@@ -58,4 +60,31 @@ async function getPlanets() {
58
60
}
59
61
60
62
getPlanets()
61
-
```
63
+
```
64
+
65
+
## 4. Error Handling in Async Functions
66
+
67
+
What if the asynchronous operation, if that promise is not resolved?, What if it's rejected?
0 commit comments