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
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.
Copy file name to clipboardExpand all lines: readme.md
+26-13Lines changed: 26 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1189,30 +1189,43 @@ For classes understanding:
1189
1189
1190
1190
### Async Await
1191
1191
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*.
1193
1193
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))
> **Note :** You must understand what are promises and how they work before trying to understand async / await since they rely on it.
1202
1197
1203
-
constuserPromise=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.
1205
1199
1206
-
#### Explanation
1200
+
#### Explanation with sample code
1201
+
1202
+
*Async / Await* is built on promises but they allow a more imperative style of code.
1207
1203
1208
1204
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.
1209
1205
1206
+
```js
1207
+
asyncfunctiongetGithubUser(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
0 commit comments