Skip to content

Commit afc5fd5

Browse files
committed
Translate article
1 parent 1094287 commit afc5fd5

1 file changed

Lines changed: 61 additions & 61 deletions

File tree

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

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ Here's the picture (compare it with the chaining above):
8383

8484
در عمل ما به ندرت چند مدیریت‌کننده برای یک promise نیاز داریم. زنجیره‌ای کردن خیلی بیشتر استفاده می‌شود.
8585

86-
## Returning promises
86+
## برگرداندن promiseها
8787

88-
A handler, used in `.then(handler)` may create and return a promise.
88+
یک مدیریت‌کننده (handler) که در `.then(handler)` استفاده شده ممکن است یک promise تولید کند و آن را برگرداند.
8989

90-
In that case further handlers wait until it settles, and then get its result.
90+
در این صورت مدیریت‌کننده‌های بعدی تا زمانی که آن تسویه شود صبر می‌کنند و سپس نتیجه آن را دریافت می‌کنند.
9191

92-
For instance:
92+
برای مثال:
9393

9494
```js run
9595
new Promise(function(resolve, reject) {
@@ -121,15 +121,15 @@ new Promise(function(resolve, reject) {
121121
});
122122
```
123123

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` را نمایش و کار یکسانی را انجام می‌دهد.
125125

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` یک ثانیه تاخیر وجود دارد.
127127

128-
Returning promises allows us to build chains of asynchronous actions.
128+
برگرداندن promiseها به ما امکان ساخت زنجیره‌هایی از عملیات ناهمگام را می‌دهد.
129129

130-
## Example: loadScript
130+
## مثال: loadScript
131131

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) تعریف شد استفاده کنیم تا اسکریپت‌ها را یکی یکی و به ترتیب بارگیری کنیم:
133133

134134
```js run
135135
loadScript("/article/promise-chaining/one.js")
@@ -140,40 +140,40 @@ loadScript("/article/promise-chaining/one.js")
140140
return loadScript("/article/promise-chaining/three.js");
141141
})
142142
.then(function(script) {
143-
// use functions declared in scripts
144-
// to show that they indeed loaded
143+
// از تابع‌های تعریف شده در اسکریپت‌ها استفاده می‌کنیم
144+
// تا نشان دهیم آن‌ها واقعا بارگیری شده‌اند
145145
one();
146146
two();
147147
three();
148148
});
149149
```
150150

151-
This code can be made bit shorter with arrow functions:
151+
این کد می‌تواند با استفاده از تابع‌های کمانی کمی کوتاه‌تر شود:
152152

153153
```js run
154154
loadScript("/article/promise-chaining/one.js")
155155
.then(script => loadScript("/article/promise-chaining/two.js"))
156156
.then(script => loadScript("/article/promise-chaining/three.js"))
157157
.then(script => {
158-
// scripts are loaded, we can use functions declared there
158+
// اسکریپت‌ها بارگیری شده‌اند، ما می‌توانیم از تابع‌هایی که آنجا تعریف شده‌اند استفاده کنیم
159159
one();
160160
two();
161161
three();
162162
});
163163
```
164164

165165

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 شد اجرا می‌شود. سپس بارگیری اسکریپت بعدی را آغاز می‌کند. پس اسکریپت‌ها یکی پس از دیگری بارگیری می‌شوند.
167167

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» است - به سمت پایین رشد می‌کند، نه به سمت راست. نشانه‌ای از «هرم عذاب وجود ندارد.
169169

170-
Technically, we could add `.then` directly to each `loadScript`, like this:
170+
از لحاظ فنی، ما می‌توانستیم `.then` را به طور مستقیم به هر `loadScript` اضافه کنیم، مثلا اینگونه:
171171

172172
```js run
173173
loadScript("/article/promise-chaining/one.js").then(script1 => {
174174
loadScript("/article/promise-chaining/two.js").then(script2 => {
175175
loadScript("/article/promise-chaining/three.js").then(script3 => {
176-
// this function has access to variables script1, script2 and script3
176+
// دسترسی دارد script3 و script2 ،script1 این تابع به متغیرهای
177177
one();
178178
two();
179179
three();
@@ -182,19 +182,19 @@ loadScript("/article/promise-chaining/one.js").then(script1 => {
182182
});
183183
```
184184

185-
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ها داریم.
186186

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ها را شروع می‌کنند گاهی اوقات درباره زنجیره‌سازی نمی‌دانند پس کد را اینگونه می‌نویسند. به طور کلی، زنجیره‌سازی ترجیح داده می‌شود.
188188

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` دسترسی دارد. اما این بیشتر از آن که یک قانون باشد، یک استثنا است.
190190

191191

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 رفتار می‌شود.
194194
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` را پیاده‌سازی می‌کنند.
196196
197-
Here's an example of a thenable object:
197+
اینجا مثالی از یک شیء thenable داریم:
198198
199199
```js run
200200
class Thenable {
@@ -203,7 +203,7 @@ class Thenable {
203203
}
204204
then(resolve, reject) {
205205
alert(resolve); // function() { native code }
206-
// resolve with this.num*2 after the 1 second
206+
// می‌شود resolve بعد از 1 ثانیه this.num*2 با
207207
setTimeout(() => resolve(this.num * 2), 1000); // (**)
208208
}
209209
}
@@ -214,70 +214,70 @@ new Promise(resolve => resolve(1))
214214
return new Thenable(result); // (*)
215215
*/!*
216216
})
217-
.then(alert); // shows 2 after 1000ms
217+
.then(alert); // بعد از 1000 میلی ثانیه 2 را نشان می‌دهد
218218
```
219219
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 ثانیه فراخوانی شده است `(**)`. سپس نتیجه به پایین زنجیره پاس داده می‌شود.
221221
222-
This feature allows us to integrate custom objects with promise chains without having to inherit from `Promise`.
222+
این ویژگی به ما اجازه می‌دهد که شیءهای شخصی‌سازی را با زنجیره‌های promise بدون اینکه اجباری به ارث‌بری از `Promise` داشته باشیم ادغام کنیم.
223223
````
224224

225225

226-
## Bigger example: fetch
226+
## مثال بزرگتر: fetch
227227

228-
In frontend programming promises are often used for network requests. So let's see an extended example of that.
228+
در برنامه‌نویسی فرانت‌اند، اغلب اوقات promiseها برای درخواست‌های شبکه استفاده می‌شوند. پس بیایید یک مثال گسترده از آن ببینیم.
229229

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) پوشش داده شده‌اند اما سینتکس پایه آن بسیار ساده است:
231231

232232
```js
233233
let promise = fetch(url);
234234
```
235235

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 تسویه می‌شود اما *قبل از اینکه تمام پاسخ دانلود شود*.
237237

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 می‌شود.
239239

240-
The code below makes a request to `user.json` and loads its text from the server:
240+
کد پایین یک درخواست به user.json می‌فرستد و متن آن را از سرور بارگیری می‌کند:
241241

242242
```js run
243243
fetch('/article/promise-chaining/user.json')
244-
// .then below runs when the remote server responds
244+
// زیر زمانی که سرور ریموت پاسخ می‌دهد اجرا می‌شود .then
245245
.then(function(response) {
246-
// response.text() returns a new promise that resolves with the full response text
247-
// when it loads
246+
// جدید برمی‌گرداند promise زمانی که بارگیری می‌شود، یک response.text()
247+
// می‌شود resolve که همراه با متن کامل پاسخ
248248
return response.text();
249249
})
250250
.then(function(text) {
251-
// ...and here's the content of the remote file
251+
// ...و اینجا محتوای فایل ریموت را داریم
252252
alert(text); // {"name": "iliakan", "isAdmin": true}
253253
});
254254
```
255255

256-
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() هم دارد که داده ریموت را می‌خواند و آن را به صورت جی‌سان می‌کند. در این مورد ما، این حتی مناسب‌تر است پس بیایید به آن سوییچ کنیم.
257257

258-
We'll also use arrow functions for brevity:
258+
ما از تابع‌های کمانی هم برای ساده‌بودن استفاده خواهیم کرد:
259259

260260
```js run
261-
// same as above, but response.json() parses the remote content as JSON
261+
// محتوای ریموت را به صورت جی‌سان تجزیه می‌کند response.json() درست مانند کد بالا اما
262262
fetch('/article/promise-chaining/user.json')
263263
.then(response => response.json())
264-
.then(user => alert(user.name)); // iliakan, got user name
264+
.then(user => alert(user.name)); // iliakan ،اسم کاربر را گرفتیم
265265
```
266266

267-
Now let's do something with the loaded user.
267+
حالا بیایید با کاربر بارگیری شده کاری کنیم.
268268

269-
For instance, we can make one more request to GitHub, load the user profile and show the avatar:
269+
برای مثال، می‌توانیم یک درخواست دیگر به GitHub بفرستیم، پروفایل کاربر را بارگیری کنیم و آواتار را نمایش دهیم:
270270

271271
```js run
272-
// Make a request for user.json
272+
// می‌سازیم user.json یک درخواست برای
273273
fetch('/article/promise-chaining/user.json')
274-
// Load it as json
274+
// آن را به صورت جی‌سان بارگیری می‌کنیم
275275
.then(response => response.json())
276-
// Make a request to GitHub
276+
// یک درخواست می‌فرستیم GitHub به
277277
.then(user => fetch(`https://api.github.com/users/${user.name}`))
278-
// Load the response as json
278+
// پاسخ را به صورت جی‌سان بارگیری می‌کنیم
279279
.then(response => response.json())
280-
// Show the avatar image (githubUser.avatar_url) for 3 seconds (maybe animate it)
280+
// (کنیم animate شاید آن را) را برای 3 ثانیه نمایش می‌دهیم (githubUser.avatar_url) تصویر آواتار
281281
.then(githubUser => {
282282
let img = document.createElement('img');
283283
img.src = githubUser.avatar_url;
@@ -288,13 +288,13 @@ fetch('/article/promise-chaining/user.json')
288288
});
289289
```
290290

291-
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ها کرده‌اند.
292292

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+
به خط (*) نگاه کنید: چگونه می‌توانیم *بعد* از اینکه نمایش آواتار تمام شد و حذف شد کاری را انجام دهیم؟ برای مثال، ما می‌خواهیم فرمی را برای ویرایش آن کاربر نشان دهیم یا چیز دیگری. تا اینجای کار، راهی وجود ندارد.
294294

295-
To make the chain extendable, we need to return a promise that resolves when the avatar finishes showing.
295+
برای اینکه زنجیره را قابل گسترش کنیم، نیاز داریم که یک promise برگردانیم تا هنگامی که نمایش آواتار تمام شد resolve شود.
296296

297-
Like this:
297+
مثلا اینگونه:
298298

299299
```js run
300300
fetch('/article/promise-chaining/user.json')
@@ -316,15 +316,15 @@ fetch('/article/promise-chaining/user.json')
316316
*/!*
317317
}, 3000);
318318
}))
319-
// triggers after 3 seconds
319+
// بعد از 3 ثانیه فعال می‌شود
320320
.then(githubUser => alert(`Finished showing ${githubUser.name}`));
321321
```
322322

323-
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 بعدی در زنجیره برای آن صبر خواهد کرد.
324324

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 برگرداند. این باعث می‌شود که بتوان بعد از آن عملیاتی را برنامه‌ریزی کرد؛ حتی اگر نخواهیم زنجیره را الان گسترش دهیم، ممکن است بعدا به آن نیاز داشته باشیم.
326326

327-
Finally, we can split the code into reusable functions:
327+
در نهایت، می‌توانیم کد را به تابع‌های قابل استفاده دوباره تقسیم کنیم:
328328

329329
```js run
330330
function loadJson(url) {
@@ -350,18 +350,18 @@ function showAvatar(githubUser) {
350350
});
351351
}
352352

353-
// Use them:
353+
// :استفاده از آن‌ها
354354
loadJson('/article/promise-chaining/user.json')
355355
.then(user => loadGithubUser(user.name))
356356
.then(showAvatar)
357357
.then(githubUser => alert(`Finished showing ${githubUser.name}`));
358358
// ...
359359
```
360360

361-
## Summary
361+
## خلاصه
362362

363-
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 برگرداند، بقیه زنجیره تا زمانی که آن تسویه شود منتظر می‌مانند. زمانی که تشویه شد، نتیجه آن (یا ارور) به بعدی‌ها پاس داده می‌شود.
364364

365-
Here's a full picture:
365+
اینجا تصویر آن را داریم:
366366

367367
![](promise-handler-variants.svg)

0 commit comments

Comments
 (0)