File tree Expand file tree Collapse file tree 4 files changed +18
-18
lines changed Expand file tree Collapse file tree 4 files changed +18
-18
lines changed Original file line number Diff line number Diff line change 1
1
2
- There are no tricks here. Just replace ` .catch ` with ` try...catch ` inside ` demoGithubUser ` and add ` async/await ` where needed :
2
+ ここには細工はありません。単に ` demoGithubUser ` の中の ` .catch ` を ` try...catch ` に置き換え、必要な場所に ` async/await ` を追加しています :
3
3
4
4
``` js run
5
5
class HttpError extends Error {
@@ -19,7 +19,7 @@ async function loadJson(url) {
19
19
}
20
20
}
21
21
22
- // Ask for a user name until github returns a valid user
22
+ // gitub が有効なユーザを返すまでユーザ名を訪ねる
23
23
async function demoGithubUser () {
24
24
25
25
let user;
@@ -28,13 +28,13 @@ async function demoGithubUser() {
28
28
29
29
try {
30
30
user = await loadJson (` https://api.github.com/users/${ name} ` );
31
- break ; // no error, exit loop
31
+ break ; // エラーがない場合、ループを抜ける
32
32
} catch (err) {
33
33
if (err instanceof HttpError && err .response .status == 404 ) {
34
- // loop continues after the alert
34
+ // alert の後ループを続ける
35
35
alert (" No such user, please reenter." );
36
36
} else {
37
- // unknown error, rethrow
37
+ // 未知のエラー、再スロー
38
38
throw err;
39
39
}
40
40
}
Original file line number Diff line number Diff line change 1
1
2
- # Rewrite "rethrow" async/await
2
+ # "再スロー" を書き直す async/await
3
3
4
- Below you can find the "rethrow" example from the chapter < info:promise-chaining > . Rewrite it using ` async/await ` instead of ` .then/catch ` .
4
+ 下にチャプター < info:promise-chaining > にある "再スロー" の例があります。 ` .then/catch ` の代わりに ` async/await ` を使って書き直してください。
5
5
6
- And get rid of the recursion in favour of a loop in ` demoGithubUser ` : with ` async/await ` that becomes easy to do.
6
+ また、 ` demoGithubUser ` のループのために( ` async/await ` が簡単になるよう)再帰を取り除きます。
7
7
8
8
``` js run
9
9
class HttpError extends Error {
@@ -25,7 +25,7 @@ function loadJson(url) {
25
25
})
26
26
}
27
27
28
- // Ask for a user name until github returns a valid user
28
+ // gitub が有効なユーザを返すまでユーザ名を訪ねる
29
29
function demoGithubUser () {
30
30
let name = prompt (" Enter a name?" , " iliakan" );
31
31
Original file line number Diff line number Diff line change 1
1
2
- The notes are below the code :
2
+ 補足はコードの下にあります :
3
3
4
4
``` js run
5
5
async function loadJson (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 ` loadUrl ` 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 . 関数 ` loadUrl ` は ` async ` になります。
23
+ 2 . すべての内側の ` .then ` は ` await ` に置き換えられます。
24
+ 3 . 次のように、await するのではなく、 ` response.json() ` を返すこともできます。 :
25
25
26
26
``` js
27
27
if (response .status == 200 ) {
28
28
return response .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
+ そうすると、外側のコードはその promise を解決するために ` await` する必要があります。
33
+ 4. ` loadJson` からスローされたエラーは ` .catch` で処理されます。そこでは ` await loadJson(…)` を使うことができません。なぜなら ` async` 関数の中ではないからです。
Original file line number Diff line number Diff line change 1
1
2
- # Rewrite using async/await
2
+ # async/await を使用して書き直す
3
3
4
- Rewrite the one of examples from the chapter < info:promise-chaining > using ` async/await ` instead of ` .then/catch ` :
4
+ チャプター < info:promise-chaining > にある例の1つを ` .then/catch ` の代わりに ` async/await ` を使って書き直してください。:
5
5
6
6
``` js run
7
7
function loadJson (url ) {
You can’t perform that action at this time.
0 commit comments