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: 1-js/11-async/08-async-await/01-rewrite-async/solution.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
2
-
The notes are below the code:
2
+
Комментарии к решению под кодом:
3
3
4
4
```js run
5
5
asyncfunctionloadJson(url) { // (1)
@@ -17,17 +17,17 @@ loadJson('no-such-user.json')
17
17
.catch(alert); // Error: 404 (4)
18
18
```
19
19
20
-
Notes:
20
+
Комментарии:
21
21
22
-
1.The function `loadJson`becomes `async`.
23
-
2.All`.then`inside are replaced with`await`.
24
-
3.We can `return response.json()` instead of awaiting for it, like this:
22
+
1.Функция `loadJson`теперь асинхронная.
23
+
2.Все`.then`внутри неё заменены на`await`.
24
+
3.Можно было бы просто вернуть промис во внешний код `return response.json()`, вот так:
25
25
26
26
```js
27
27
if (response.status==200) {
28
28
returnresponse.json(); // (3)
29
29
}
30
30
```
31
31
32
-
Then the outer code would have to `await`for that promise to resolve. In our case it doesn't matter.
33
-
4. The error thrown from `loadJson` is handled by `.catch`. We can't use`await loadJson(…)` there, because we're not in an `async` function.
32
+
Тогда внешнему коду пришлось бы получать результат промиса самостоятельно (через `.then` или `await`). В нашем варианте это не обязательно.
33
+
4.Выброшенная из `loadJson`ошибка перехватывается с помощью `.catch`. Здесь нельзя использовать`await loadJson(…)`, так как мы находимся не в теле функции `async`.
0 commit comments