Skip to content
Merged

Fetch #264

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
14 changes: 7 additions & 7 deletions 5-network/01-fetch/01-fetch-users/solution.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

To fetch a user we need: `fetch('https://api.github.com/users/USERNAME')`.
برای دریافت کاربران به این موارد نیاز داریم: `fetch('https://api.github.com/users/USERNAME')`.

If the response has status `200`, call `.json()` to read the JS object.
اگر پاسخ دارای وضعیت `200` است،برای خواندن شی `.json()` را فراخوانی کنید.

Otherwise, if a `fetch` fails, or the response has non-200 status, we just return `null` in the resulting array.
در غیر اینصورت اگر `fetch` ناموفق بود یا پاسخ دارای وضعیت غیر 200 (200-299) بود در آرایه نتیجه `null` قرار میگیرد.

So here's the code:
پس کد به اینصورت است:

```js demo
async function getUsers(names) {
Expand Down Expand Up @@ -33,8 +33,8 @@ async function getUsers(names) {
}
```

Please note: `.then` call is attached directly to `fetch`, so that when we have the response, it doesn't wait for other fetches, but starts to read `.json()` immediately.
لطفا توجه کنید: فراخوانی `.then` به صورت مستقیم به `fetch` متصل شده است، بنابراین وقتی پاسخ را داریم، منتظر دیگر fetch ها نمی‌ماند، بلکه به طور فوری شروع به خواندن `.json()` میکند.

If we used `await Promise.all(names.map(name => fetch(...)))`, and call `.json()` on the results, then it would wait for all fetches to respond. By adding `.json()` directly to each `fetch`, we ensure that individual fetches start reading data as JSON without waiting for each other.
اگر از `await Promise.all(names.map(name => fetch(...)))` استفاده کنیم و `.json()` را برروی نتایج فراخوانی کنیم، آنگاه باید منتظر بماند تا همه درخواست‌ها پاسخ دهند. با اضافه کردن `.json()` به صورت مستقیم به هر `fetch` اطمینان حاصل می‌کنیم که هر کدام آنها به صورت جداگانه شروع به خواندن داده‌ها به صورت JSON می‌کند و منتظر یکدیگر نمی‌ماند.

That's an example of how low-level Promise API can still be useful even if we mainly use `async/await`.
این مثال نشان‌دهنده این است که چگونه متدهای Promise در سطح پایین همچنان می‌تواند مفید باشد حتی اگر بیشتر از `async/await` استفاده شود.
16 changes: 8 additions & 8 deletions 5-network/01-fetch/01-fetch-users/task.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Fetch users from GitHub
# دریافت کاربران از گیت هاب

Create an async function `getUsers(names)`, that gets an array of GitHub logins, fetches the users from GitHub and returns an array of GitHub users.
یک تابع async به نام `getUsers(names)`بسازید که یک آرایه از نام‌های ورود به سیستم گیت هاب دریافت کرده، کاربران را از گیت هاب بارگیری کند و یک آرایه از کاربران گیت هاب را برگرداند.

The GitHub url with user information for the given `USERNAME` is: `https://api.github.com/users/USERNAME`.
آدرس گیت هاب برای دریافت اطلاعات کاربران `USERNAME` به این صورت است: `https://api.github.com/users/USERNAME`.

There's a test example in the sandbox.
در سندباکس (sandbox) یک مثال آزمایشی وجود دارد

Important details:
موارد مهم:

1. There should be one `fetch` request per user.
2. Requests shouldn't wait for each other. So that the data arrives as soon as possible.
3. If any request fails, or if there's no such user, the function should return `null` in the resulting array.
1. باید یک درخواست `fetch` برای هر کاربر وجود داشته باشد.
2. درخواست‌ها باید منتظر یکدیگر نباشند تا داده‌ها به سرعت برسند.
3. اگر هر درخواست موفق آمیز نباشد یا چنین کاربری وجود نداشته باشد،تابع باید مقدار `null` را در آرایه نتیجه برگرداند.
Loading