Skip to content

Async/await #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions 1-js/11-async/08-async-await/01-rewrite-async/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

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

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

Notes:
نکات:

1. The function `loadJson` becomes `async`.
2. All `.then` inside are replaced with `await`.
3. We can `return response.json()` instead of awaiting for it, like this:
۱. تابع `loadJson` به `async` تغییر کرد.

۲. تمام `then.` ها با `await` جایگزین شده اند.

۳. ما می توانیم بجای صبر کردن برای نتیجه، مستقیما آن `return response.json()` را برگردانیم؛ مانند زیر:

```js
if (response.status == 200) {
return response.json(); // (3)
}
```

Then the outer code would have to `await` for that promise to resolve. In our case it doesn't matter.
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.
بنابراین کد بیرونی باید برای اجرا شدن Promise از `await` استفاده کند. در مثال ما خیلی اهمیت ندارد.
۵. خطای ایجاد شده از `loadJson` توسط `catch.` مدیریت می شود. ما نمی توانیم به صورت `(...)await loadJson` استفاده کنیم، زیرا ما در هیچ تابع `async` ای نیستیم.
4 changes: 2 additions & 2 deletions 1-js/11-async/08-async-await/01-rewrite-async/task.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

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

Rewrite this example code from the chapter <info:promise-chaining> using `async/await` instead of `.then/catch`:
مثال کد زیر از بخش <info:promise-chaining> با استفاده از `async/await` بجای `then/catch.` بازنویسی کنید:

```js run
function loadJson(url) {
Expand Down
10 changes: 5 additions & 5 deletions 1-js/11-async/08-async-await/02-rewrite-async-2/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

There are no tricks here. Just replace `.catch` with `try..catch` inside `demoGithubUser` and add `async/await` where needed:
ترفند خاصی وجود ندارد. فقط `catch.` را با `try..catch` در داخل `demoGithubUser` جایگزین کنید و `async/await` را در جایی که نیاز است اضافه کنید:

```js run
class HttpError extends Error {
Expand All @@ -19,7 +19,7 @@ async function loadJson(url) {
}
}

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

let user;
Expand All @@ -28,13 +28,13 @@ async function demoGithubUser() {

try {
user = await loadJson(`https://api.github.com/users/${name}`);
break; // no error, exit loop
break; // خطایی رخ نداده است، از حلقه خارج می شود
} catch(err) {
if (err instanceof HttpError && err.response.status == 404) {
// loop continues after the alert
// حلقه بعد از alert ادامه می یابد
alert("No such user, please reenter.");
} else {
// unknown error, rethrow
// خطای ناشناخته، مجدد throw می شود
throw err;
}
}
Expand Down
8 changes: 4 additions & 4 deletions 1-js/11-async/08-async-await/02-rewrite-async-2/task.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

# Rewrite "rethrow" with async/await
# بازنویسی "rethrow" با async/await

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

And get rid of the recursion in favour of a loop in `demoGithubUser`: with `async/await` that becomes easy to do.
و از حالت بازگشتی در `demoGithubUser` خلاص شوید: با استفاده از `async/await` بسیار آسان می شود.

```js run
class HttpError extends Error {
Expand All @@ -25,7 +25,7 @@ function loadJson(url) {
});
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

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

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

function f() {
// shows 10 after 1 second
// بعد از ۱ ثانیه ۱۰ را نشان می دهند‍‍
*!*
wait().then(result => alert(result));
*/!*
Expand Down
12 changes: 6 additions & 6 deletions 1-js/11-async/08-async-await/03-async-from-regular/task.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

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

We have a "regular" function called `f`. How can you call the `async` function `wait()` and use its result inside of `f`?
ما یک تابع معمولی داریم که `f` نامگذاری شده است. چگونه می توانید تابع `()wait` که `async` است را فراخوانی کنید و از نتیجه آن داخل `f` استفاده کنید؟

```js
async function wait() {
Expand All @@ -11,10 +11,10 @@ async function wait() {
}

function f() {
// ...what should you write here?
// we need to call async wait() and wait to get 10
// remember, we can't use "await"
// ... چی باید اینچا بنویسیم?
// ما باید async wait() را فراخوانی کنیم و صبر کنیم تا ۱۰ را بگیریم
// به یاد داشته باشید، نمی توانیم از "await" استفاده کنیم
}
```

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