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
+61-61Lines changed: 61 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -83,13 +83,13 @@ Here's the picture (compare it with the chaining above):
83
83
84
84
در عمل ما به ندرت چند مدیریتکننده برای یک promise نیاز داریم. زنجیرهای کردن خیلی بیشتر استفاده میشود.
85
85
86
-
## Returning promises
86
+
## برگرداندن promiseها
87
87
88
-
A handler, used in`.then(handler)`may create and return a promise.
88
+
یک مدیریتکننده (handler) که در`.then(handler)`استفاده شده ممکن است یک promise تولید کند و آن را برگرداند.
89
89
90
-
In that case further handlers wait until it settles, and then get its result.
90
+
در این صورت مدیریتکنندههای بعدی تا زمانی که آن تسویه شود صبر میکنند و سپس نتیجه آن را دریافت میکنند.
91
91
92
-
For instance:
92
+
برای مثال:
93
93
94
94
```js run
95
95
newPromise(function(resolve, reject) {
@@ -121,15 +121,15 @@ new Promise(function(resolve, reject) {
121
121
});
122
122
```
123
123
124
-
Here the first `.then`shows`1`and returns `new Promise(…)`in the line `(*)`. After one second it resolves, and the result (the argument of `resolve`, here it's `result * 2`) is passed on to the handler of the second `.then`. That handler is in the line `(**)`, it shows`2`and does the same thing.
124
+
اینجا اولین `.then`مقدار`1`را نشان میدهد و در خط `(*)`مقدار `new Promise(…)` را برمیگرداند. بعد از یک ثانیه resolve میشود و نتیجه (آرگومان `resolve`، اینجا `result * 2` است) را به مدیریتکننده از `.then` دوم پاس میدهد. آن مدیریتکننده در خط `(**)` است و`2`را نمایش و کار یکسانی را انجام میدهد.
125
125
126
-
So the output is the same as in the previous example: 1 -> 2 -> 4, but now with 1 second delay between `alert`calls.
126
+
پس خروجی مانند مثال قبل یکسان است: 1 -> 2 -> 4 اما حالا بین فراخوانیهای `alert`یک ثانیه تاخیر وجود دارد.
127
127
128
-
Returning promises allows us to build chains of asynchronous actions.
128
+
برگرداندن promiseها به ما امکان ساخت زنجیرههایی از عملیات ناهمگام را میدهد.
129
129
130
-
## Example: loadScript
130
+
## مثال: loadScript
131
131
132
-
Let's use this feature with the promisified `loadScript`, defined in the [previous chapter](info:promise-basics#loadscript), to load scripts one by one, in sequence:
132
+
بیایید از این ویژگی با `loadScript` که بر اساس promise است و در [فصل قبل](info:promise-basics#loadscript) تعریف شد استفاده کنیم تا اسکریپتها را یکی یکی و به ترتیب بارگیری کنیم:
//scripts are loaded, we can use functions declared there
158
+
//اسکریپتها بارگیری شدهاند، ما میتوانیم از تابعهایی که آنجا تعریف شدهاند استفاده کنیم
159
159
one();
160
160
two();
161
161
three();
162
162
});
163
163
```
164
164
165
165
166
-
Here each `loadScript`call returns a promise, and the next `.then`runs when it resolves. Then it initiates the loading of the next script. So scripts are loaded one after another.
166
+
اینجا هر فراخوانی `loadScript`یک promise برمیگرداند و `.then`بعدی زمانی که آن resolve شد اجرا میشود. سپس بارگیری اسکریپت بعدی را آغاز میکند. پس اسکریپتها یکی پس از دیگری بارگیری میشوند.
167
167
168
-
We can add more asynchronous actions to the chain. Please note that the code is still "flat" — it grows down, not to the right. There are no signs of the "pyramid of doom".
168
+
ما میتوانیم کارهای ناهمگام بیشتری را به زنجیره اضافه کنیم. لطفا توجه کنید که کد هنوز «flat» است - به سمت پایین رشد میکند، نه به سمت راست. نشانهای از «هرم عذاب وجود ندارد.
169
169
170
-
Technically, we could add `.then`directly to each `loadScript`, like this:
170
+
از لحاظ فنی، ما میتوانستیم `.then`را به طور مستقیم به هر `loadScript` اضافه کنیم، مثلا اینگونه:
This code does the same: loads 3 scripts in sequence. But it "grows to the right". So we have the same problem as with callbacks.
185
+
این کد کار یکسانی را انجام میدهد: 3 اسکریپت را به ترتیب بارگیری میکند. اما «به سمت راست رشد میکند». پس مشکلی یکسان با callbackها داریم.
186
186
187
-
People who start to use promises sometimes don't know about chaining, so they write it this way. Generally, chaining is preferred.
187
+
کسانی که استفاده از promiseها را شروع میکنند گاهی اوقات درباره زنجیرهسازی نمیدانند پس کد را اینگونه مینویسند. به طور کلی، زنجیرهسازی ترجیح داده میشود.
188
188
189
-
Sometimes it's ok to write `.then`directly, because the nested function has access to the outer scope. In the example above the most nested callback has access to all variables `script1`,`script2`, `script3`. But that's an exception rather than a rule.
189
+
گاهی نوشتن `.then`به صورت مستقیم مشکلی ندارد چون تابع تودرتو به محدوده بیرونی دسترسی دارد. در مثال بالا تودرتوترین callback به تمام متغیر های `script1`،`script2` و `script3` دسترسی دارد. اما این بیشتر از آن که یک قانون باشد، یک استثنا است.
190
190
191
191
192
-
````smart header="Thenables"
193
-
To be precise, a handler may return not exactly a promise, but a so-called "thenable" object - an arbitrary object that has a method `.then`. It will be treated the same way as a promise.
192
+
````smart header="Thenableها"
193
+
اگر بخواهیم دقیق باشیم، یک مدیریتکننده ممکن است دقیقا یک promise برنگرداند اما شیءای به اصطلاح "thenable" را برگرداند - یک شیء دلخواه که متد `.then` را دارد. با این شیء درست مانند یک promise رفتار میشود.
194
194
195
-
The idea is that 3rd-party libraries may implement "promise-compatible" objects of their own. They can have an extended set of methods, but also be compatible with native promises, because they implement `.then`.
195
+
ایده این است که کتابخانههای شخص ثالث ممکن است شیءهای «سازگار با promise» خودشان را پیادهسازی کنند. این شیءها ممکن است مجموعهای از متدهای خودشان را داشته باشند اما با promiseها نیز سازگار باشند چون آنها `.then` را پیادهسازی میکنند.
@@ -214,70 +214,70 @@ new Promise(resolve => resolve(1))
214
214
return new Thenable(result); // (*)
215
215
*/!*
216
216
})
217
-
.then(alert); // shows 2 after 1000ms
217
+
.then(alert); // بعد از 1000 میلی ثانیه 2 را نشان میدهد
218
218
```
219
219
220
-
JavaScript checks the object returned by the `.then` handler in line `(*)`: if it has a callable method named `then`, then it calls that method providing native functions `resolve`, `reject` as arguments (similar to an executor) and waits until one of them is called. In the example above `resolve(2)` is called after 1 second `(**)`. Then the result is passed further down the chain.
220
+
جاوااسکریپت در خط `(*)` شیء برگردانده شده توسط مدیریتکننده `.then` را بررسی میکند: اگر متدی قابل فراخوانی به نام `then` دارد، سپس آن متد را با فراهم کردن تابعهای نیتیو `resolve` و `reject` به عنوان آرگومان فراخوانی میکند (مانند یک اجرا کننده) و تا زمانی که یکی از آنها فراخوانی شود صبر میکند. در مثال بالا `resolve(2)` بعد از 1 ثانیه فراخوانی شده است `(**)`. سپس نتیجه به پایین زنجیره پاس داده میشود.
221
221
222
-
This feature allows us to integrate custom objects with promise chains without having to inherit from `Promise`.
222
+
این ویژگی به ما اجازه میدهد که شیءهای شخصیسازی را با زنجیرههای promise بدون اینکه اجباری به ارثبری از `Promise` داشته باشیم ادغام کنیم.
223
223
````
224
224
225
225
226
-
## Bigger example: fetch
226
+
## مثال بزرگتر: fetch
227
227
228
-
In frontend programming promises are often used for network requests. So let's see an extended example of that.
228
+
در برنامهنویسی فرانتاند، اغلب اوقات promiseها برای درخواستهای شبکه استفاده میشوند. پس بیایید یک مثال گسترده از آن ببینیم.
229
229
230
-
We'll use the[fetch](info:fetch)method to load the information about the user from the remote server. It has a lot of optional parameters covered in [separate chapters](info:fetch), but the basic syntax is quite simple:
230
+
ما از متد[fetch](info:fetch)برای اینکه اطلاعات کاربر را از سرور ریموت بارگیری کنیم استفاده خواهیم کرد. این متد پارامترهای اختیاری زیادی دارد که در [فصلهای جداگانه](info:fetch) پوشش داده شدهاند اما سینتکس پایه آن بسیار ساده است:
231
231
232
232
```js
233
233
let promise =fetch(url);
234
234
```
235
235
236
-
This makes a network request to the `url` and returns a promise. The promise resolves with a `response` object when the remote server responds with headers, but *before the full response is downloaded*.
236
+
این یک درخواست شبکهای به url میفرستد و یک promise را برمیگرداند. زمانی که سرور همراه با headerها پاسخ میدهد، promise همراه با یک شیء response تسویه میشود اما *قبل از اینکه تمام پاسخ دانلود شود*.
237
237
238
-
To read the full response, we should call the method `response.text()`: it returns a promise that resolves when the full text is downloaded from the remote server, with that text as a result.
238
+
برای خواندن پاسخ کامل، ما باید متد response.text() را فراخوانی کنیم: این متد یک promise برمیگرداند که بعد از دانلود شدن کامل متن از سرور ریموت، همراه با متن به عنوان نتیجه resolve میشود.
239
239
240
-
The code below makes a request to `user.json` and loads its text from the server:
240
+
کد پایین یک درخواست به user.json میفرستد و متن آن را از سرور بارگیری میکند:
241
241
242
242
```js run
243
243
fetch('/article/promise-chaining/user.json')
244
-
//.then below runs when the remote server responds
244
+
//زیر زمانی که سرور ریموت پاسخ میدهد اجرا میشود .then
245
245
.then(function(response) {
246
-
//response.text() returns a new promise that resolves with the full responsetext
247
-
//when it loads
246
+
//جدید برمیگرداند promise زمانی که بارگیری میشود، یک response.text()
The `response` object returned from `fetch` also includes the method `response.json()` that reads the remote data and parses it as JSON. In our case that's even more convenient, so let's switch to it.
256
+
شیء response که از fetch برگردانده شده است متد response.json() هم دارد که داده ریموت را میخواند و آن را به صورت جیسان میکند. در این مورد ما، این حتی مناسبتر است پس بیایید به آن سوییچ کنیم.
257
257
258
-
We'll also use arrow functions for brevity:
258
+
ما از تابعهای کمانی هم برای سادهبودن استفاده خواهیم کرد:
259
259
260
260
```js run
261
-
//same as above, but response.json() parses the remote content as JSON
261
+
//محتوای ریموت را به صورت جیسان تجزیه میکند response.json() درست مانند کد بالا اما
262
262
fetch('/article/promise-chaining/user.json')
263
263
.then(response=>response.json())
264
-
.then(user=>alert(user.name)); // iliakan, got user name
264
+
.then(user=>alert(user.name)); // iliakan ،اسم کاربر را گرفتیم
265
265
```
266
266
267
-
Now let's do something with the loaded user.
267
+
حالا بیایید با کاربر بارگیری شده کاری کنیم.
268
268
269
-
For instance, we can make one more request to GitHub, load the user profile and show the avatar:
269
+
برای مثال، میتوانیم یک درخواست دیگر به GitHub بفرستیم، پروفایل کاربر را بارگیری کنیم و آواتار را نمایش دهیم:
The code works; see comments about the details. However, there's a potential problem in it, a typical error for those who begin to use promises.
291
+
این کد کار میکند؛ برای دانستن جزئیات کامنتها را بخوانید. اگرچه، یک مشکل احتمالی درون آن وجود دارد، یک ارور معمول برای کسانی که شروع به استفاده از promiseها کردهاند.
292
292
293
-
Look at the line `(*)`: how can we do something *after* the avatar has finished showing and gets removed? For instance, we'd like to show a form for editing that user or something else. As of now, there's no way.
293
+
به خط (*) نگاه کنید: چگونه میتوانیم *بعد* از اینکه نمایش آواتار تمام شد و حذف شد کاری را انجام دهیم؟ برای مثال، ما میخواهیم فرمی را برای ویرایش آن کاربر نشان دهیم یا چیز دیگری. تا اینجای کار، راهی وجود ندارد.
294
294
295
-
To make the chain extendable, we need to return a promise that resolves when the avatar finishes showing.
295
+
برای اینکه زنجیره را قابل گسترش کنیم، نیاز داریم که یک promise برگردانیم تا هنگامی که نمایش آواتار تمام شد resolve شود.
That is, the `.then` handler in line `(*)` now returns `new Promise`, that becomes settled only after the call of `resolve(githubUser)` in `setTimeout``(**)`. The next `.then` in the chain will wait for that.
323
+
یعنی اینکه مدیریتکننده .then در خط (*) حالا یک new Promise برمیگرداند که فقط بعد از فراخوانی resolve(githubUser) در setTimeout خط (**) تسویه میشود. .then بعدی در زنجیره برای آن صبر خواهد کرد.
324
324
325
-
As a good practice, an asynchronous action should always return a promise. That makes it possible to plan actions after it; even if we don't plan to extend the chain now, we may need it later.
325
+
به عنوان یک عادت خوب، یک عمل ناهنگام باید همیشه یک promise برگرداند. این باعث میشود که بتوان بعد از آن عملیاتی را برنامهریزی کرد؛ حتی اگر نخواهیم زنجیره را الان گسترش دهیم، ممکن است بعدا به آن نیاز داشته باشیم.
326
326
327
-
Finally, we can split the code into reusable functions:
327
+
در نهایت، میتوانیم کد را به تابعهای قابل استفاده دوباره تقسیم کنیم:
328
328
329
329
```js run
330
330
functionloadJson(url) {
@@ -350,18 +350,18 @@ function showAvatar(githubUser) {
If a `.then` (or`catch/finally`, doesn't matter) handler returns a promise, the rest of the chain waits until it settles. When it does, its result (or error) is passed further.
363
+
اگر مدیریتکننده یک `.then` (یا`catch/finally`، مهم نیست) یک promise برگرداند، بقیه زنجیره تا زمانی که آن تسویه شود منتظر میمانند. زمانی که تشویه شد، نتیجه آن (یا ارور) به بعدیها پاس داده میشود.
0 commit comments