Skip to content

Commit ba1f910

Browse files
committed
Translate a part of article
1 parent d6aea13 commit ba1f910

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

1-js/11-async/03-promise-chaining/article.md

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

2-
# Promises chaining
2+
# زنجیره‌ای کردن Promise
33

4-
Let's return to the problem mentioned in the chapter <info:callbacks>: we have a sequence of asynchronous tasks to be performed one after another — for instance, loading scripts. How can we code it well?
4+
بیایید به مشکلی که در فصل <info:callbacks> ذکر شد برگردیم: ما دنباله‌ای از کارهای ناهمگام داریم که یکی پس از دیگری اجرا شوند - برای مثال، بارگیری اسکریپت‌ها. چگونه می‌توانیم آن را به خوبی کدنویسی کنیم؟
55

6-
Promises provide a couple of recipes to do that.
6+
Promiseها چند دستورالعمل برای انجام آن فراهم می‌کنند.
77

8-
In this chapter we cover promise chaining.
8+
در این فصل ما زنجیره‌ای کردن promise را پوشش می‌دهیم.
99

10-
It looks like this:
10+
اینگونه بنظر می‌رسد:
1111

1212
```js run
1313
new Promise(function(resolve, reject) {
@@ -32,25 +32,25 @@ new Promise(function(resolve, reject) {
3232
});
3333
```
3434

35-
The idea is that the result is passed through the chain of `.then` handlers.
35+
ایده کار این است که نتیجه از طریق زنجیره‌ای از مدیریت‌کننده‌های `.then` پاس داده شود.
3636

37-
Here the flow is:
38-
1. The initial promise resolves in 1 second `(*)`,
39-
2. Then the `.then` handler is called `(**)`, which in turn creates a new promise (resolved with `2` value).
40-
3. The next `then` `(***)` gets the result of the previous one, processes it (doubles) and passes it to the next handler.
41-
4. ...and so on.
37+
اینجا روند برنامه اینگونه است:
38+
1. شیء promise اول در 1 ثانیه resolve می‌شود `(*)`.
39+
2. سپس مدیریت‌کننده `.then` فراخوانی می‌شود `(**)` که به نوبه خود یک promise جدید می‌سازد (که با مقدار `2` حل‌وفصل می‌شود).
40+
3. `then` بعدی `(***)` نتیجه قبلی را دریافت می‌کند، آن را پردازش می‌کند (دو برابرش می‌کند) و آن را به مدیریت‌کننده بعدی انتقال می‌دهد.
41+
4. ...و این چرخه ادامه دارد.
4242

43-
As the result is passed along the chain of handlers, we can see a sequence of `alert` calls: `1` -> `2` -> `4`.
43+
همانطور که نتیجه در طول زنجیره مدیریت‌کننده‌ها پاس داده می‌شود، ما می‌توانیم دنباله‌ای از فراخوانی‌های `alert` را ببینیم: `1` -> `2` -> `4`.
4444

4545
![](promise-then-chain.svg)
4646

47-
The whole thing works, because every call to a `.then` returns a new promise, so that we can call the next `.then` on it.
47+
تمام این کد کار می‌کند چون هر فراخوانی `.then` یک promise جدید برمی‌گرداند پس ما می‌توانیم `.then` بعدی را روی آن فراخوانی کنیم.
4848

49-
When a handler returns a value, it becomes the result of that promise, so the next `.then` is called with it.
49+
زمانی که یک مدیریت‌کننده مقداری را برمی‌گرداند، این مقدار به نتیجه آن promise تبدیل می‌شود پس `.then` بعدی همراه آن فراخوانی می‌شود.
5050

51-
**A classic newbie error: technically we can also add many `.then` to a single promise. This is not chaining.**
51+
**یک ارور کلاسیک افراد تازه‌کار: از لحاظ فنی ما می‌توانیم تعداد زیادی `.then` را هم به یک promise اضافه کنیم. این کار زنجیره‌ای کردن نیست.**
5252

53-
For example:
53+
برای مثال:
5454
```js run
5555
let promise = new Promise(function(resolve, reject) {
5656
setTimeout(() => resolve(1), 1000);
@@ -72,15 +72,16 @@ promise.then(function(result) {
7272
});
7373
```
7474

75-
What we did here is just several handlers to one promise. They don't pass the result to each other; instead they process it independently.
75+
کاری که اینجا کردیم فقط اضافه کردن چند مدیریت‌کننده به یک promise است. آن‌ها نتیجه را به یکدیگر پاس نمی‌دهند؛ در عوض به صورت جداگانه آن را پردازش می‌کنند.
7676

7777
Here's the picture (compare it with the chaining above):
78+
تصویر را اینجا داریم (آن را با زنجیره‌ای کردن بالا مقایسه کنید):
7879

7980
![](promise-then-many.svg)
8081

81-
All `.then` on the same promise get the same result -- the result of that promise. So in the code above all `alert` show the same: `1`.
82+
تمام `.then` ها روی promise یکسان نتیجه یکسانی دریافت می‌کنند -- نتیجه همان promise. پس در کد بالا تمام `alert`ها مقدار یکسانی را نمایش می‌دهند: `1`.
8283

83-
In practice we rarely need multiple handlers for one promise. Chaining is used much more often.
84+
در عمل ما به ندرت چند مدیریت‌کننده برای یک promise نیاز داریم. زنجیره‌ای کردن خیلی بیشتر استفاده می‌شود.
8485

8586
## Returning promises
8687

0 commit comments

Comments
 (0)