Skip to content

Commit 975eb2d

Browse files
authored
Improvements on description, code and resources
I tried to make a more "engligh" description of the concept, fixed the sample code that wasn't working (API url was wrong) and added a try / catch to show how error handling works. Besides, I added some external resources I found great.
1 parent ba0ee50 commit 975eb2d

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

readme.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,30 +1189,43 @@ For classes understanding:
11891189

11901190
### Async Await
11911191

1192-
The `async function` declaration defines an asynchronous function, which returns and `AsyncFunction` object. `await` is operator which is used to wait for a `Promise`. The `await` operator can only be used inside `async` function.
1192+
In addition to [Promises](#promises), there is a new syntax you might encounter to handle asynchronous code named *async / await*.
11931193

1194-
#### Sample Code
1194+
The purpose of async/await functions is to simplify the behavior of using promises synchronously and to perform some behavior on a group of Promises. Just as Promises are similar to structured callbacks, async/await is similar to combining generators and promises. ([Ref: MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function))
11951195

1196-
```js
1197-
async function getGithubUser(handle) {
1198-
const url = `https://api.github.com/api/users/${handle}`;
1199-
const response = await fetch(url);
1200-
return await response.json();
1201-
}
1196+
> **Note :** You must understand what are promises and how they work before trying to understand async / await since they rely on it.
12021197
1203-
const userPromise = getGithubUser('user123');
1204-
```
1198+
> **Note 2:** [*await* must be used in an *async* function](https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9#f3f0), which means that you can't use await in the top level of our code since that is not inside an async function.
12051199
1206-
#### Explanation
1200+
#### Explanation with sample code
1201+
1202+
*Async / Await* is built on promises but they allow a more imperative style of code.
12071203

12081204
An `await` expression causes `async` function to pause the execution, wait for promise to resolve, and then resume the execution once the value is resolved. Any `async` function returns the `Promise`, and will be resolved to returned value.
12091205

1206+
```js
1207+
async function getGithubUser(handle) { // async keyword allows usage of await in the function and means function returns a promise
1208+
try { // this is how errors are handled with async / await
1209+
const url = `https://api.github.com/users/${handle}`;
1210+
const response = await fetch(url); // "synchronously" waiting fetch promise to resolve before going to next line
1211+
return response.json();
1212+
} catch (err) {
1213+
alert(err);
1214+
}
1215+
}
1216+
1217+
getGithubUser('mbeaudru').then(user => console.log(user)); // logging user response - cannot use await syntax since this code isn't in async function
1218+
```
1219+
12101220
#### External resources
12111221

1212-
- [Async Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
1213-
- [Await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
1222+
- [Async/Await - JavaScript.Info](https://javascript.info/async-await)
12141223
- [ES7 Async/Await](http://rossboucher.com/await/#/)
12151224
- [6 Reasons Why JavaScript’s Async/Await Blows Promises Away](https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9)
1225+
- [JavaScript awaits](https://dev.to/kayis/javascript-awaits)
1226+
- [Using Async Await in Express with Node 8](https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016)
1227+
- [Async Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
1228+
- [Await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
12161229

12171230
## Glossary
12181231

0 commit comments

Comments
 (0)