You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/11-async/03-promise-chaining/article.md
+20-19Lines changed: 20 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
2
-
# Promises chaining
2
+
# زنجیرهای کردن Promise
3
3
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> ذکر شد برگردیم: ما دنبالهای از کارهای ناهمگام داریم که یکی پس از دیگری اجرا شوند - برای مثال، بارگیری اسکریپتها. چگونه میتوانیم آن را به خوبی کدنویسی کنیم؟
5
5
6
-
Promises provide a couple of recipes to do that.
6
+
Promiseها چند دستورالعمل برای انجام آن فراهم میکنند.
7
7
8
-
In this chapter we cover promise chaining.
8
+
در این فصل ما زنجیرهای کردن promise را پوشش میدهیم.
9
9
10
-
It looks like this:
10
+
اینگونه بنظر میرسد:
11
11
12
12
```js run
13
13
newPromise(function(resolve, reject) {
@@ -32,25 +32,25 @@ new Promise(function(resolve, reject) {
32
32
});
33
33
```
34
34
35
-
The idea is that the result is passed through the chain of `.then`handlers.
35
+
ایده کار این است که نتیجه از طریق زنجیرهای از مدیریتکنندههای `.then`پاس داده شود.
36
36
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. ...و این چرخه ادامه دارد.
42
42
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`.
44
44
45
45

46
46
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`بعدی را روی آن فراخوانی کنیم.
48
48
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`بعدی همراه آن فراخوانی میشود.
50
50
51
-
**A classic newbie error: technically we can also add many `.then`to a single promise. This is not chaining.**
51
+
**یک ارور کلاسیک افراد تازهکار: از لحاظ فنی ما میتوانیم تعداد زیادی `.then`را هم به یک promise اضافه کنیم. این کار زنجیرهای کردن نیست.**
52
52
53
-
For example:
53
+
برای مثال:
54
54
```js run
55
55
let promise =newPromise(function(resolve, reject) {
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 است. آنها نتیجه را به یکدیگر پاس نمیدهند؛ در عوض به صورت جداگانه آن را پردازش میکنند.
76
76
77
77
Here's the picture (compare it with the chaining above):
78
+
تصویر را اینجا داریم (آن را با زنجیرهای کردن بالا مقایسه کنید):
78
79
79
80

80
81
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`.
82
83
83
-
In practice we rarely need multiple handlers for one promise. Chaining is used much more often.
84
+
در عمل ما به ندرت چند مدیریتکننده برای یک promise نیاز داریم. زنجیرهای کردن خیلی بیشتر استفاده میشود.
0 commit comments