Skip to content

Commit b50c5d0

Browse files
authored
Merge pull request #277 from D0x45/master
Microtasks
2 parents 63683bf + 070005a commit b50c5d0

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
11

22
# Microtasks
33

4-
Promise handlers `.then`/`.catch`/`.finally` are always asynchronous.
4+
هندلرهای (handler) پرامیس `.then`/`.catch`/`.finally` همواره ناهمگام هستند.
55

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` هستند هنوز پیش از این هندلرها (handler) اجرا می شوند.
77

8-
Here's a demo:
8+
یک نمونه:
99

1010
```js run
1111
let promise = Promise.resolve();
1212

1313
promise.then(() => alert("promise done!"));
1414

15-
alert("code finished"); // this alert shows first
15+
alert("code finished"); // ابتدا این هشدار نمایان می شود
1616
```
1717

18-
If you run it, you see `code finished` first, and then `promise done!`.
18+
اگر این کد را اجرا کنید, عبارت `code finished` را در ابتدا و سپس `promise done!` را میبینید.
1919

20-
That's strange, because the promise is definitely done from the beginning.
20+
این عجیب است, زیرا پرامیس قطعا از پیش به انجام رسیده است.
2121

22-
Why did the `.then` trigger afterwards? What's going on?
22+
چرا `.then` بعدتر اجرا شد؟ چه رخ میدهد؟
2323

24-
## Microtasks queue
24+
## صف Microtasks
2525

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).
26+
کار های ناهمگام نیازمند مدیریت درست هستند. به همین سبب، استاندارد ECMA یک صف داخلی به نام `PromiseJobs` مشخص میکند که بیشتر با نام "microtask queue" از آن یاد می شود (اصطلاح V8).
2727

28-
As stated in the [specification](https://tc39.github.io/ecma262/#sec-jobs-and-job-queues):
28+
همانطور که در [خصوصیات زبان](https://tc39.github.io/ecma262/#sec-jobs-and-job-queues) یاد شده:
2929

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.
30+
- صف first-in-first-out است: کارهایی که نخست وارد صف شده اند نخست اجرا می شوند.
31+
- اجرای یک کار تنها زمانی شروع می شود که چیز دیگری در حال اجرا نباشد.
3232

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.
33+
یا، به عبارت ساده تر، زمانی که یک پرامیس آماده است، مدیر های `.then/catch/finally` آن درون صف قرار داده می شوند؛ آنها هنوز اجرا نشده اند. زمانی که موتور جاوااسکریپت از کد فعلی رها می شود، یک کار (تسک) از صف میگیرد و آن را اجرا میکند.
3434

35-
That's why "code finished" in the example above shows first.
35+
به همین دلیل عبارت "code finished" در نمونه بالا نخست نمایان می شود.
3636

3737
![](promiseQueue.svg)
3838

39-
Promise handlers always go through this internal queue.
39+
هندلرهای (handler) پرامیس همواره از درون این صف داخلی می گذرند.
4040

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.
41+
اگر زنجیره ای با چندین `.then/catch/finally` باشد، آنگاه هر یک از آنها به صورت ناهمگام اجرا می شود. بدان صورت که، ابتدا وارد صف می شود، سپس زمانی که کد فعلی تمام شده و هندلرهای (handler) پیشین صف شده به پایان رسیده اند، اجرا می شود.
4242

43-
**What if the order matters for us? How can we make `code finished` appear after `promise done`?**
43+
**اگر ترتیب برای ما اهمیت داشت چه? چگونه می توانیم `code finished` را پیش از `promise done` نمایان کنیم؟**
4444

45-
Easy, just put it into the queue with `.then`:
45+
به سادگی، فقط با استفاده از `.then` درون صف قرارش بده:
4646

4747
```js run
4848
Promise.resolve()
4949
.then(() => alert("promise done!"))
5050
.then(() => alert("code finished"));
5151
```
5252

53-
Now the order is as intended.
53+
حالا ترتیب در نظر گرفته شده.
5454

55-
## Unhandled rejection
55+
## rejection مدیریت نشده
5656

57-
Remember the `unhandledrejection` event from the article <info:promise-error-handling>?
57+
ایونت `unhandledrejection` را از مقاله <info:promise-error-handling> به یاد دارید؟
5858

59-
Now we can see exactly how JavaScript finds out that there was an unhandled rejection.
59+
حال میتوانیم به دقت ببینیم که جاوااسکریپت چگونه پی میبرد که رد شدن مدیریت نشده ای پیش آمده.
6060

61-
**An "unhandled rejection" occurs when a promise error is not handled at the end of the microtask queue.**
61+
**یک "rejection مدیریت نشده" زمانی پیش می آید که یک خطای پرامیس در پایان صف خرده کار مدیریت نشده باشد.**
6262

63-
Normally, if we expect an error, we add `.catch` to the promise chain to handle it:
63+
معمولا، اگر منتظر خطایی هستیم، `.catch` را به زنجیره پرامیس می افزاییم تا آن را مدیریت کند:
6464

6565
```js run
6666
let promise = Promise.reject(new Error("Promise Failed!"));
6767
*!*
6868
promise.catch(err => alert('caught'));
6969
*/!*
7070

71-
// doesn't run: error handled
71+
// اجرا نمی شود: خطا مدیریت شد
7272
window.addEventListener('unhandledrejection', event => alert(event.reason));
7373
```
7474

75-
But if we forget to add `.catch`, then, after the microtask queue is empty, the engine triggers the event:
75+
اما اگر فراموش کنیم که `.catch` را بیافزاییم، آنگاه، پس از اینکه صف خرده کار خالی شد، موتور جاوااسکریپت، ایونت را فراخوانی میکند:
7676

7777
```js run
7878
let promise = Promise.reject(new Error("Promise Failed!"));
@@ -81,7 +81,7 @@ let promise = Promise.reject(new Error("Promise Failed!"));
8181
window.addEventListener('unhandledrejection', event => alert(event.reason));
8282
```
8383

84-
What if we handle the error later? Like this:
84+
چه می شود اگر خطا را بعدتر مدیریت کنیم؟ مثل این کد:
8585

8686
```js run
8787
let promise = Promise.reject(new Error("Promise Failed!"));
@@ -93,20 +93,20 @@ setTimeout(() => promise.catch(err => alert('caught')), 1000);
9393
window.addEventListener('unhandledrejection', event => alert(event.reason));
9494
```
9595

96-
Now, if we run it, we'll see `Promise Failed!` first and then `caught`.
96+
حال، اگر اجرایش کنیم، عبارت `Promise Failed!` را نخست و سپس عبارت `caught` را مشاهده خواهیم کرد.
9797

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!"
98+
اگر ما درباره صف Microtasks نمیدانستیم، می توانستیم شگفت زده شویم: "چرا هندلر `unhandledrejection` اجرا شد؟ ما یقینا خطا را گرفتیم و مدیریت کردیم!"
9999

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.
100+
اما حال متوجه می شویم که `unhandledrejection` زمانی ایجاد می شود که Microtask کار تمام شده: موتور جاوااسکریپت پرامیس ها را بررسی میکند و، اگر هر یک از آنها در وضعیت "rejected" باشد، آنگاه ایونت فراخوانی می شود.
101101

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.
102+
در نمونه بالا، `.catch` افزوده شده توسط `setTimeout` هم فراخوانی می شود. اما بعدتر فراخوانی می شود، پس از اینکه دیگر `unhandledrejection` رخ داده، پس چیزی را تغییر نمیدهد.
103103

104-
## Summary
104+
## چکیده
105105

106-
Promise handling is always asynchronous, as all promise actions pass through the internal "promise jobs" queue, also called "microtask queue" (V8 term).
106+
مدیریت پرامیس همواره ناهمگام است، همانطور که تمام عملیات پرامیس از درون صف داخلی "promise jobs" می گذرند، همچنین با عنوان "microtask queue" از آن یاد می شود (اصطلاح V8).
107107

108-
So `.then/catch/finally` handlers are always called after the current code is finished.
108+
پس هندلر های `.then/catch/finally` همواره پس از به پایان رسیدن کد فعلی فراخوانی می شوند.
109109

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.
110+
اگر نیاز داریم که تضمین کنیم یک تکه کد پس از `.then/catch/finally` اجرا می شود، می توانیم به یک فراخوانی زنجیره ای `.then` آن را بیافزاییم.
111111

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>.
112+
در بیشتر موتور های جاوااسکریپت، شامل مرورگر ها و Node.js، مفهوم Microtasks به دقت با "event loop" و "macrotasks" در هم تنیده شده. از آنجایی که این دو رابطه مستقیمی با پرامیس ها ندارند، در بخش دیگری از این دوره به آنها پرداخته شده، در مقاله <info:event-loop>.

0 commit comments

Comments
 (0)