Skip to content

Commit 05c9ecc

Browse files
authored
Add Async await and other examples.
1 parent f12649b commit 05c9ecc

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ One place JavaScript!
77
- [Design Pattern](#pattern)
88
- [Callback/ Higher-order Function](#callback)
99
- [Promises](#promises)
10+
- [Async/Await](#async)
1011
- [Closures](#closures)
1112
- [Pure functions](#pure)
1213
- [Splice vs Slice vs Split](#diffs)
@@ -69,6 +70,8 @@ https://www.dofactory.com/javascript/singleton-design-pattern
6970

7071
#### Callback/ Higher-order Function
7172

73+
This is the old-fashioned classical approach to asynchronous programming. You provide a function as an argument to another function that executes an asynchronous task. When the asynchronous task completes, the executing function calls your callback function.
74+
7275
A callback function is a function passed into another function as an argument,
7376
which is then invoked inside the outer function to complete some kind of action.
7477
It is also known as Higher-order Function.
@@ -82,23 +85,62 @@ pair of executing parenthesis () like we do when we are executing a function.
8285

8386
Callback functions Are **Closures.** Callback function is executed inside the containing function’s body just as if
8487
the callback were defined in the containing function. Hence callback function has access to the function variables as
85-
well as global variables.
88+
well as global variables.
89+
90+
The disadvantage of using callback occurs when you have multiple chained async tasks, which requires you to define callback inside callback, inside callback and that result in **callback hell**
8691

8792
[JsFiddle Example](http://jsfiddle.net/varit05/o3vu14kd/)
8893

8994
<a name=“promises”/>
9095

9196
#### Promises
9297

98+
Promises have been introduced in ES6 (2015) to allow for more readable asynchronous code than is possible with callbacks.
99+
93100
The promise constructor takes one argument, a callback with two parameters, resolve and reject.
94101
Do something within the callback, perhaps async, then call resolve if everything worked, otherwise call reject.
95102

103+
The main difference between callbacks and promises is that with callbacks you tell the executing function
104+
what to do when the asynchronous task completes, whereas with promises the executing function returns a special
105+
object to you (the promise) and then you tell the promise what to do when the asynchronous task completes.
106+
96107
A Promise can be:
97108
1. fulfilled - The action relating to the promise succeeded
98109
2. rejected - The action relating to the promise failed
99110
3. pending - Hasn't fulfilled or rejected yet
100111
4. settled - Has fulfilled or rejected
101112

113+
```javascript
114+
let promise = new Promise(function(resolve, reject) {
115+
resolve(1);
116+
});
117+
118+
promise.then(data=> console.log(data));
119+
```
120+
121+
122+
<a name=“async”/>
123+
124+
#### Async/ Await
125+
126+
Async/await has been introduced in ES8 (2017). This technique should really be listed under promises, because it is just syntactic sugar for working with promises. However, it is a syntactic sugar that is really worth looking at.
127+
128+
```javascript
129+
async function f() {
130+
try {
131+
let response = await fetch('https://jsonplaceholder.typicode.com/posts');
132+
let user = await response.json();
133+
console.log(user);
134+
} catch(err) {
135+
// catches errors both in fetch and response.json
136+
alert(err);
137+
}
138+
}
139+
140+
f();
141+
142+
```
143+
102144
<a name=“closures”/>
103145

104146
#### Closures

0 commit comments

Comments
 (0)