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: README.md
+25-23Lines changed: 25 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -1267,45 +1267,45 @@ For classes understanding:
1267
1267
1268
1268
In addition to [Promises](#promises), there is a new syntax you might encounter to handle asynchronous code named *async / await*.
1269
1269
1270
-
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))
1270
+
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. Async functions *always* returns a Promise. ([Ref: MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function))
1271
1271
1272
-
> **Note :** You must understand what are promises and how they work before trying to understand async / await since they rely on it.
1272
+
> **Note :** You must understand what promises are and how they work before trying to understand async / await since they rely on it.
1273
1273
1274
1274
> **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.
1275
1275
1276
1276
#### Sample code
1277
1277
1278
1278
```js
1279
1279
asyncfunctiongetGithubUser(username) { // async keyword allows usage of await in the function and means function returns a promise
1280
-
constresponse=awaitfetch(`https://api.github.com/users/${username}`); //"synchronously" waiting fetch promise to resolve before going to next line
1280
+
constresponse=awaitfetch(`https://api.github.com/users/${username}`); //Execution is paused here until the Promise returned by fetch is resolved
1281
1281
returnresponse.json();
1282
1282
}
1283
1283
1284
1284
getGithubUser('mbeaudru')
1285
1285
.then(user=>console.log(user)) // logging user response - cannot use await syntax since this code isn't in async function
1286
-
.catch(err=>console.log(err)); // if an error is raised in our async function, we will catch it here
1286
+
.catch(err=>console.log(err)); // if an error is thrown in our async function, we will catch it here
1287
1287
```
1288
1288
1289
1289
#### Explanation with sample code
1290
1290
1291
1291
*Async / Await* is built on promises but they allow a more imperative style of code.
1292
1292
1293
-
*async* operator turns a function into a *promise* in which you can use the *await* operator.
1293
+
The *async* operator marks a function as asynchronous and will always return a *Promise*. You can use the *await* operator in an *async* function to pause execution on that line until the returned Promise from the expression either resolves or rejects.
1294
1294
1295
1295
```js
1296
1296
asyncfunctionmyFunc() {
1297
1297
// we can use await operator because this function is async
1298
1298
return"hello world";
1299
1299
}
1300
1300
1301
-
myFunc().then(msg=>console.log(msg)) // "hello world" -- myFunc is turned into a promise because of async operator
1301
+
myFunc().then(msg=>console.log(msg)) // "hello world" -- myFunc's return value is turned into a promise because of async operator
1302
1302
```
1303
1303
1304
-
When the *return* statement of an async function is reached, the promise is fulfilled with the value returned. If an error is thrown inside an async function, the promise state will turn to *rejected*.
1304
+
When the *return* statement of an async function is reached, the Promise is fulfilled with the value returned. If an error is thrown inside an async function, the Promise state will turn to *rejected*. If no value is returned from an async function, a Promise is still returned and resolves with no value when execution of the async function is complete.
1305
1305
1306
-
*await* operator is used to wait for a *Promise* to be fulfilled and only can be used inside an *async* function body. When encountered, the code execution is paused until the promise is fulfilled.
1306
+
*await* operator is used to wait for a *Promise* to be fulfilled and can only be used inside an *async* function body. When encountered, the code execution is paused until the promise is fulfilled.
1307
1307
1308
-
> **Note :***fetch* is a Promise that allows to do an AJAX request
1308
+
> **Note :***fetch* is a function that returns a Promise that allows to do an AJAX request
1309
1309
1310
1310
Let's see how we could fetch a github user with promises first:
1311
1311
@@ -1323,7 +1323,7 @@ Here's the *async / await* equivalent:
Unless we add *try / catch* blocks around *await* expressions, uncaught exceptions – regardless of whether they were raised in the body of your *async* function or while it’s suspended during *await* – will reject the promise returned by the *async* function. [(Ref: PonyFoo)](https://ponyfoo.com/articles/understanding-javascript-async-await#error-handling).
1358
+
Unless we add *try / catch* blocks around *await* expressions, uncaught exceptions – regardless of whether they were thrown in the body of your *async* function or while it’s suspended during *await* – will reject the promise returned by the *async* function. Using the `throw` statement in an async function is the same as returning a Promise that rejects. [(Ref: PonyFoo)](https://ponyfoo.com/articles/understanding-javascript-async-await#error-handling).
1357
1359
1358
1360
> **Note :** Promises behave the same!
1359
1361
1360
1362
With promises, here is how you would handle the error chain:
1361
1363
1362
1364
```js
1363
1365
functiongetUser() { // This promise will be rejected!
1364
-
returnnewPromise((res, rej) =>rej("User not found !"))
1365
-
};
1366
+
returnnewPromise((res, rej) =>rej("User not found !"));
0 commit comments