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
مدیریت کننده های پرامیس `.then`/`.catch`/`.finally`همواره ناهمگام هستند.
5
5
6
-
Even when a Promise is immediately resolved, the code on the lines *below*`.then`/`.catch`/`.finally`will still execute before these handlers.
6
+
حتی زمانی که یک پرامیس در آن واحد به سر انجام رسیده, کد هایی که در خطوط *زیرین*`.then`/`.catch`/`.finally`هستند هنوز پیش از این مدیریت کننده ها اجرا می شوند.
7
7
8
-
Here's a demo:
8
+
یک نمونه:
9
9
10
10
```js run
11
11
let promise =Promise.resolve();
12
12
13
13
promise.then(() =>alert("promise done!"));
14
14
15
-
alert("code finished"); //this alert shows first
15
+
alert("code finished"); //این هشدار پیشتر نمایان می شود
16
16
```
17
+
اگر این کد را اجرا کنید, عبارت `code finished` را در ابتدا و سپس `promise done!` را میبینید.
17
18
18
-
If you run it, you see `code finished` first, and then `promise done!`.
19
+
این عجیب است, زیرا پرامیس قطعا از پیش به انجام رسیده است.
19
20
20
-
That's strange, because the promise is definitely done from the beginning.
21
+
چرا `.then` بعدتر اجرا شد؟ چه رخ میدهد؟
21
22
22
-
Why did the `.then` trigger afterwards? What's going on?
23
+
## صف خرده کار ها
23
24
24
-
## Microtasks queue
25
+
کار های ناهمگام نیازمند مدیریت درست هستند. به همین سبب، استاندارد ECMA یک صف داخلی به نام `PromiseJobs` مشخص میکند که بیشتر با نام "microtask queue" از آن یاد می شود (اصطلاح V8).
25
26
26
-
Asynchronous tasks need proper management. For that, the ECMA standard specifies an internal queue `PromiseJobs`, more often referred to as the "microtask queue" (V8 term).
27
+
همانطور که در [خصوصیات](https://tc39.github.io/ecma262/#sec-jobs-and-job-queues) یاد شده:
27
28
28
-
As stated in the [specification](https://tc39.github.io/ecma262/#sec-jobs-and-job-queues):
29
+
- صف first-in-first-out است: کارهایی که نخست وارد صف شده اند نخست اجرا می شوند.
30
+
- اجرای یک کار تنها زمانی شروع می شود که چیز دیگری در حال اجرا نباشد.
29
31
30
-
- The queue is first-in-first-out: tasks enqueued first are run first.
31
-
- Execution of a task is initiated only when nothing else is running.
32
+
یا، به عبارت ساده تر، زمانی که یک پرامیس آماده است، مدیر های `.then/catch/finally` آن درون صف قرار داده می شوند؛ آنها هنوز اجرا نشده اند. زمانی که موتور جاوااسکریپت از کد فعلی رها می شود، یک کار (تسک) از صف میگیرد و آن را اجرا میکند.
32
33
33
-
Or, to put it more simply, when a promise is ready, its `.then/catch/finally` handlers are put into the queue; they are not executed yet. When the JavaScript engine becomes free from the current code, it takes a task from the queue and executes it.
34
-
35
-
That's why "code finished" in the example above shows first.
34
+
به همین دلیل عبارت "code finished" در نمونه بالا نخست نمایان می شود.
36
35
37
36

38
37
39
-
Promise handlers always go through this internal queue.
38
+
مدیریت کننده های پرامیس همواره از درون این صف داخلی می گذرند.
40
39
41
-
If there's a chain with multiple `.then/catch/finally`, then every one of them is executed asynchronously. That is, it first gets queued, then executed when the current code is complete and previously queued handlers are finished.
40
+
اگر زنجیره ای با چندین `.then/catch/finally` باشد، آنگاه هر یک از آنها به صورت ناهمگام اجرا می شود. بدان صورت که، ابتدا وارد صف می شود، سپس زمانی که کد فعلی تمام شده و مدیریت کننده های پیشین صف شده به پایان رسیده اند، اجرا می شود.
42
41
43
-
**What if the order matters for us? How can we make `code finished`appear after `promise done`?**
42
+
**اگر ترتیب برای ما اهمیت داشت چه? چگونه می توانیم `code finished`را پیش از `promise done` نمایان کنیم؟**
44
43
45
-
Easy, just put it into the queue with `.then`:
44
+
به سادگی، فقط با استفاده از `.then` درون صف قرارش بده:
46
45
47
46
```js run
48
47
Promise.resolve()
49
48
.then(() =>alert("promise done!"))
50
49
.then(() =>alert("code finished"));
51
50
```
52
51
53
-
Now the order is as intended.
52
+
حالا ترتیب در نظر گرفته شده.
54
53
55
-
## Unhandled rejection
54
+
## رد شدن مدیریت نشده
56
55
57
-
Remember the `unhandledrejection`event from the article <info:promise-error-handling>?
56
+
رخداد `unhandledrejection`را از مقاله <info:promise-error-handling> به یاد دارید؟
58
57
59
-
Now we can see exactly how JavaScript finds out that there was an unhandled rejection.
58
+
حال میتوانیم به دقت ببینیم که جاوااسکریپت چگونه پی میبرد که رد شدن مدیریت نشده ای پیش آمده.
60
59
61
-
**An "unhandled rejection" occurs when a promise error is not handled at the end of the microtask queue.**
60
+
**یک "رد شدن مدیریت نشده" زمانی پیش می آید که یک حطای پرامیس در پایان صف خرده کار مدیریت نشده باشد.**
62
61
63
-
Normally, if we expect an error, we add `.catch`to the promise chain to handle it:
62
+
معمولا، اگر منتظر خطایی هستیم، `.catch`را به زنجیره پرامیس می افزاییم تا آن را مدیریت کند:
64
63
65
64
```js run
66
65
let promise =Promise.reject(newError("Promise Failed!"));
Now, if we run it, we'll see `Promise Failed!`first and then `caught`.
95
+
حال، اگر اجرایش کنیم، عبارت `Promise Failed!`را نخست و سپس عبارت `caught` را مشاهده خواهیم کرد.
97
96
98
-
If we didn't know about the microtasks queue, we could wonder: "Why did`unhandledrejection`handler run? We did catch and handle the error!"
97
+
اگر ما درباره صف خرده کار ها نمیدانستیم، می توانستیم شگفت زده شویم: "چرا مدیر`unhandledrejection`اجرا شد؟ ما یقینا حطا را گرفتیم و مدیریت کردیم!"
99
98
100
-
But now we understand that `unhandledrejection`is generated when the microtask queue is complete: the engine examines promises and, if any of them is in the "rejected" state, then the event triggers.
99
+
اما حال میتوجه می شویم که `unhandledrejection`زمانی ایجاد می شود که صف خرده کار تمام شده: موتور پرامیس ها را بررسی میکند و، اگر هر یک از آنها در وضعیت "rejected" باشد، آنگاه رویداد فراخوانی می شود.
101
100
102
-
In the example above, `.catch`added by `setTimeout`also triggers. But it does so later, after `unhandledrejection`has already occurred, so it doesn't change anything.
101
+
در نمونه بالا، `.catch`افزوده شده توسط `setTimeout`هم فراخوانی می شود. اما بعدتر فراخوانی می شود، پس از اینکه دیگر `unhandledrejection`رخ داده، پس چیزی را تغییر نمیدهد.
103
102
104
-
## Summary
103
+
## چکیده
105
104
106
-
Promise handling is always asynchronous, as all promise actions pass through the internal "promise jobs" queue, also called "microtask queue" (V8 term).
105
+
مدیریت پرامیس همواره ناهمگام است، همانطور که تمام عملیات پرامیس از درون صف داخلی "promise jobs" می گذرند، همچنین با عنوان "microtask queue" از آن یاد می شود (اصطلاح V8).
107
106
108
-
So `.then/catch/finally`handlers are always called after the current code is finished.
107
+
پس مدیر های `.then/catch/finally`همواره پس از به پایان رسیدن کد فعلی فراخوانی می شوند.
109
108
110
-
If we need to guarantee that a piece of code is executed after `.then/catch/finally`, we can add it into a chained `.then`call.
109
+
اگر نیاز داریم که تضمین کنیم یک تکه کد پس از `.then/catch/finally` اجرا می شود، می توانیم به یک فراخوانی زنجیره ای `.then`بیافزاییمش.
111
110
112
-
In most Javascript engines, including browsers and Node.js, the concept of microtasks is closely tied with the "event loop" and "macrotasks". As these have no direct relation to promises, they are covered in another part of the tutorial, in the article<info:event-loop>.
111
+
در بیشتر موتور های جاوااسکریپت، شامل مرورگر ها و Node.js، مفهوم خرده کار ها به دقت با "event loop" و "macrotasks" در هم تنیده شده. از آنجایی که این دو رابطه مستقیمی با پرامیس ها ندارند، در بخش دیگری از این دوره به آنها پرداخته شده، در مقاله<info:event-loop>.
0 commit comments