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
In addition to [Promises](#promises), there is a new syntax you might encounter to handle asynchronous code named *async / await*.
1192
+
1193
+
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))
1194
+
1195
+
> **Note :** You must understand what are promises and how they work before trying to understand async / await since they rely on it.
1196
+
1197
+
> **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.
1198
+
1199
+
#### Explanation with sample code
1200
+
1201
+
*Async / Await* is built on promises but they allow a more imperative style of code.
1202
+
1203
+
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.
1204
+
1205
+
```js
1206
+
asyncfunctiongetGithubUser(handle) { // async keyword allows usage of await in the function and means function returns a promise
1207
+
try { // this is how errors are handled with async / await
0 commit comments