Skip to content

Commit 47df4fa

Browse files
authored
Merge pull request #219 from ARTM2000/feat/async-await
Async/await
2 parents c8b74e3 + 7943f0e commit 47df4fa

File tree

7 files changed

+94
-92
lines changed

7 files changed

+94
-92
lines changed

1-js/11-async/08-async-await/01-rewrite-async/solution.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
The notes are below the code:
2+
نکات پایین کد قرار دارند:
33

44
```js run
55
async function loadJson(url) { // (1)
@@ -17,17 +17,19 @@ loadJson('https://javascript.info/no-such-user.json')
1717
.catch(alert); // Error: 404 (4)
1818
```
1919

20-
Notes:
20+
نکات:
2121

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()` را برگردانیم؛ مانند زیر:
2527

2628
```js
2729
if (response.status == 200) {
2830
return response.json(); // (3)
2931
}
3032
```
3133

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` ای نیستیم.

1-js/11-async/08-async-await/01-rewrite-async/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
# Rewrite using async/await
2+
# بازنویسی با استفاده از async/await
33

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.` بازنویسی کنید:
55

66
```js run
77
function loadJson(url) {

1-js/11-async/08-async-await/02-rewrite-async-2/solution.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

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` را در جایی که نیاز است اضافه کنید:
33

44
```js run
55
class HttpError extends Error {
@@ -19,7 +19,7 @@ async function loadJson(url) {
1919
}
2020
}
2121

22-
// Ask for a user name until github returns a valid user
22+
// نام کاربری را می پرسد تا زمانی که گیت هاب یک کاربر معتبر برگرداند
2323
async function demoGithubUser() {
2424

2525
let user;
@@ -28,13 +28,13 @@ async function demoGithubUser() {
2828

2929
try {
3030
user = await loadJson(`https://api.github.com/users/${name}`);
31-
break; // no error, exit loop
31+
break; // خطایی رخ نداده است، از حلقه خارج می شود
3232
} catch(err) {
3333
if (err instanceof HttpError && err.response.status == 404) {
34-
// loop continues after the alert
34+
// حلقه بعد از alert ادامه می یابد
3535
alert("No such user, please reenter.");
3636
} else {
37-
// unknown error, rethrow
37+
// خطای ناشناخته، مجدد throw می شود
3838
throw err;
3939
}
4040
}

1-js/11-async/08-async-await/02-rewrite-async-2/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
# Rewrite "rethrow" with async/await
2+
# بازنویسی "rethrow" با async/await
33

4-
Below you can find the "rethrow" example. Rewrite it using `async/await` instead of `.then/catch`.
4+
در زیر ما مثالی از "rethrow" پیدا می کنیم. آن را با استفاده از ‍`async/await` بجای `then/catch.` بازنویسی کنید.
55

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` بسیار آسان می شود.
77

88
```js run
99
class HttpError extends Error {
@@ -25,7 +25,7 @@ function loadJson(url) {
2525
});
2626
}
2727

28-
// Ask for a user name until github returns a valid user
28+
// نام کاربری را می پرسد تا زمانی که گیت هاب یک کاربر معتبر برگرداند
2929
function demoGithubUser() {
3030
let name = prompt("Enter a name?", "iliakan");
3131

1-js/11-async/08-async-await/03-async-from-regular/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
That's the case when knowing how it works inside is helpful.
2+
این مورد زمانی است که دانستن نحوه عملکرد آن در داخل تابع عادی مفید است.
33

4-
Just treat `async` call as promise and attach `.then` to it:
4+
فقط کافیست که با `async` مانند Promise عمل کنیم و `then.` را به آن اضافه کنیم:
55
```js run
66
async function wait() {
77
await new Promise(resolve => setTimeout(resolve, 1000));
@@ -10,7 +10,7 @@ async function wait() {
1010
}
1111

1212
function f() {
13-
// shows 10 after 1 second
13+
// بعد از ۱ ثانیه ۱۰ را نشان می دهند‍‍
1414
*!*
1515
wait().then(result => alert(result));
1616
*/!*
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
# Call async from non-async
2+
# فراخوانی async از non-async
33

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` استفاده کنید؟
55

66
```js
77
async function wait() {
@@ -11,10 +11,10 @@ async function wait() {
1111
}
1212

1313
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" استفاده کنیم
1717
}
1818
```
1919

20-
P.S. The task is technically very simple, but the question is quite common for developers new to async/await.
20+
ضمیمه: این کار از نظر فنی بسیار ساده است اما این سوال برای توسعه دهندگان که با async/await تازه آشنا شده اند بسیار متداول است.

0 commit comments

Comments
 (0)