Skip to content

Commit 8f14501

Browse files
author
D0x45
authored
Microtasks: initial update
1 parent e7279d0 commit 8f14501

1 file changed

Lines changed: 37 additions & 38 deletions

File tree

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,77 @@
11

2-
# Microtasks
2+
# خرده کار ها
33

4-
Promise handlers `.then`/`.catch`/`.finally` are always asynchronous.
4+
مدیریت کننده های پرامیس `.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` هستند هنوز پیش از این مدیریت کننده ها اجرا می شوند.
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
```
17+
اگر این کد را اجرا کنید, عبارت `code finished` را در ابتدا و سپس `promise done!` را میبینید.
1718

18-
If you run it, you see `code finished` first, and then `promise done!`.
19+
این عجیب است, زیرا پرامیس قطعا از پیش به انجام رسیده است.
1920

20-
That's strange, because the promise is definitely done from the beginning.
21+
چرا `.then` بعدتر اجرا شد؟ چه رخ میدهد؟
2122

22-
Why did the `.then` trigger afterwards? What's going on?
23+
## صف خرده کار ها
2324

24-
## Microtasks queue
25+
کار های ناهمگام نیازمند مدیریت درست هستند. به همین سبب، استاندارد ECMA یک صف داخلی به نام `PromiseJobs` مشخص میکند که بیشتر با نام "microtask queue" از آن یاد می شود (اصطلاح V8).
2526

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) یاد شده:
2728

28-
As stated in the [specification](https://tc39.github.io/ecma262/#sec-jobs-and-job-queues):
29+
- صف first-in-first-out است: کارهایی که نخست وارد صف شده اند نخست اجرا می شوند.
30+
- اجرای یک کار تنها زمانی شروع می شود که چیز دیگری در حال اجرا نباشد.
2931

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

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" در نمونه بالا نخست نمایان می شود.
3635

3736
![](promiseQueue.svg)
3837

39-
Promise handlers always go through this internal queue.
38+
مدیریت کننده های پرامیس همواره از درون این صف داخلی می گذرند.
4039

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` باشد، آنگاه هر یک از آنها به صورت ناهمگام اجرا می شود. بدان صورت که، ابتدا وارد صف می شود، سپس زمانی که کد فعلی تمام شده و مدیریت کننده های پیشین صف شده به پایان رسیده اند، اجرا می شود.
4241

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

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

4746
```js run
4847
Promise.resolve()
4948
.then(() => alert("promise done!"))
5049
.then(() => alert("code finished"));
5150
```
5251

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

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

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

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

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

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

6564
```js run
6665
let promise = Promise.reject(new Error("Promise Failed!"));
6766
*!*
6867
promise.catch(err => alert('caught'));
6968
*/!*
7069

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

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

7776
```js run
7877
let promise = Promise.reject(new Error("Promise Failed!"));
@@ -81,7 +80,7 @@ let promise = Promise.reject(new Error("Promise Failed!"));
8180
window.addEventListener('unhandledrejection', event => alert(event.reason));
8281
```
8382

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

8685
```js run
8786
let promise = Promise.reject(new Error("Promise Failed!"));
@@ -93,20 +92,20 @@ setTimeout(() => promise.catch(err => alert('caught')), 1000);
9392
window.addEventListener('unhandledrejection', event => alert(event.reason));
9493
```
9594

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

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` اجرا شد؟ ما یقینا حطا را گرفتیم و مدیریت کردیم!"
9998

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" باشد، آنگاه رویداد فراخوانی می شود.
101100

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` رخ داده، پس چیزی را تغییر نمیدهد.
103102

104-
## Summary
103+
## چکیده
105104

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).
107106

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

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` بیافزاییمش.
111110

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

Comments
 (0)