File tree Expand file tree Collapse file tree 7 files changed +94
-92
lines changed
1-js/11-async/08-async-await Expand file tree Collapse file tree 7 files changed +94
-92
lines changed 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,19 @@ loadJson('https://javascript.info/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
+ ۱. تابع ` loadJson ` به ` async ` تغییر کرد.
23
+
24
+ ۲. تمام ` then. ` ها با ` await ` جایگزین شده اند.
25
+
26
+ ۳. ما می توانیم بجای صبر کردن برای نتیجه، مستقیما آن ` return response.json() ` را برگردانیم؛ مانند زیر:
25
27
26
28
```js
27
29
if (response.status == 200) {
28
30
return response.json(); // (3)
29
31
}
30
32
```
31
33
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 .
34
+ بنابراین کد بیرونی باید برای اجرا شدن Promise از `await` استفاده کند. در مثال ما خیلی اهمیت ندارد .
35
+ ۵. خطای ایجاد شده از ` 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 this example code from the chapter < info:promise-chaining > using ` async/await ` instead of ` . then/catch` :
4
+ مثال کد زیر از بخش < info:promise-chaining > با استفاده از ` async/await ` بجای ` then/catch. ` بازنویسی کنید :
5
5
6
6
``` js run
7
7
function loadJson (url ) {
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
+ ترفند خاصی وجود ندارد. فقط ` catch. ` را با ` try..catch ` در داخل ` demoGithubUser ` جایگزین کنید و ` 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
+ // نام کاربری را می پرسد تا زمانی که گیت هاب یک کاربر معتبر برگرداند
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
+ // خطای ناشناخته، مجدد throw می شود
38
38
throw err;
39
39
}
40
40
}
Original file line number Diff line number Diff line change 1
1
2
- # Rewrite "rethrow" with async/await
2
+ # بازنویسی "rethrow" با async/await
3
3
4
- Below you can find the "rethrow" example. Rewrite it using ` async/await ` instead of ` . then/catch` .
4
+ در زیر ما مثالی از "rethrow" پیدا می کنیم. آن را با استفاده از ` async/await ` بجای ` then/catch. ` بازنویسی کنید .
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
+ // نام کاربری را می پرسد تا زمانی که گیت هاب یک کاربر معتبر برگرداند
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
- That's the case when knowing how it works inside is helpful .
2
+ این مورد زمانی است که دانستن نحوه عملکرد آن در داخل تابع عادی مفید است .
3
3
4
- Just treat ` async ` call as promise and attach ` . then` to it :
4
+ فقط کافیست که با ` async ` مانند Promise عمل کنیم و ` then. ` را به آن اضافه کنیم :
5
5
``` js run
6
6
async function wait () {
7
7
await new Promise (resolve => setTimeout (resolve, 1000 ));
@@ -10,7 +10,7 @@ async function wait() {
10
10
}
11
11
12
12
function f () {
13
- // shows 10 after 1 second
13
+ // بعد از ۱ ثانیه ۱۰ را نشان می دهند
14
14
* ! *
15
15
wait ().then (result => alert (result));
16
16
*/ ! *
Original file line number Diff line number Diff line change 1
1
2
- # Call async from non-async
2
+ # فراخوانی async از non-async
3
3
4
- We have a "regular" function called ` f ` . How can you call the ` async ` function ` wait() ` and use its result inside of ` f ` ?
4
+ ما یک تابع معمولی داریم که ` f ` نامگذاری شده است. چگونه می توانید تابع ` ()wait ` که ` async ` است را فراخوانی کنید و از نتیجه آن داخل ` f ` استفاده کنید؟
5
5
6
6
``` js
7
7
async function wait () {
@@ -11,10 +11,10 @@ async function wait() {
11
11
}
12
12
13
13
function f () {
14
- // ...what should you write here ?
15
- // we need to call async wait() and wait to get 10
16
- // remember, we can't use "await"
14
+ // ... چی باید اینچا بنویسیم ?
15
+ // ما باید async wait() را فراخوانی کنیم و صبر کنیم تا ۱۰ را بگیریم
16
+ // به یاد داشته باشید، نمی توانیم از "await" استفاده کنیم
17
17
}
18
18
```
19
19
20
- P.S. The task is technically very simple, but the question is quite common for developers new to async/await.
20
+ ضمیمه: این کار از نظر فنی بسیار ساده است اما این سوال برای توسعه دهندگان که با async/await تازه آشنا شده اند بسیار متداول است .
You can’t perform that action at this time.
0 commit comments