Skip to content

Commit 88ed6d8

Browse files
Update article.md
translation of `generators` is completed
1 parent 7094bb2 commit 88ed6d8

1 file changed

Lines changed: 50 additions & 50 deletions

File tree

  • 1-js/12-generators-iterators/1-generators

1-js/12-generators-iterators/1-generators/article.md

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ alert(generator); // [object Generator]
5454
```
5555

5656

57-
اجرای بدنه تایع هنوز شروع نشده است:
57+
اجرای بدنه تابع هنوز شروع نشده است:
5858

5959
![](generateSequence-1.svg)
6060

@@ -137,7 +137,7 @@ alert(JSON.stringify(three)); // {value: 3, *!*done: true*/!*}
137137
138138
## generatorها iterable هستند.
139139
140-
همانطور که احتمالا با توجه به `()next` متوجه شده‌اید، generatorها [iterable](info:iterable) هستند.
140+
همان‌طور که احتمالا با توجه به `()next` متوجه شده‌اید، generatorها [iterable](info:iterable) هستند.
141141
142142
با استفاد از `for..of` می‌توان از `value` آن‌ها استفاده کرد:
143143
@@ -319,7 +319,7 @@ alert([...range]); // 1,2,3,4,5
319319

320320
```smart header="generatorها ممکن است تا ابد مقدار تولید کنند"
321321
322-
در مثال بالا، یک دنباله کران دار تولید کردیم، ولی می‌توان به همان روش یک دنباله بی‌کران از مقادیر را ساخت. مثل یک دنباله بی‌پایان از اعداد شبه تصادفی.
322+
در مثال بالا، یک دنباله کران‌دار تولید کردیم، ولی می‌توان به همان روش یک دنباله بی‌کران از مقادیر را ساخت. مثل یک دنباله بی‌پایان از اعداد شبه تصادفی.
323323
324324
```
325325

@@ -443,23 +443,23 @@ alert(str); // 0..9A..Za..z
443443

444444
ترکیب generatorها یک راه معقول برای استفاده از جریان یک generator درون دیگری است و از حافظه بیشتر برای ذخیره مقادیر استفاده نمی‌کند.
445445

446-
## "yield" is a two-way street
446+
## yield یک خیابان دو طرفه است.
447447

448-
Until this moment, generators were similar to iterable objects, with a special syntax to generate values. But in fact they are much more powerful and flexible.
448+
تا الان، generatorها بسیار شبیه به آبجکت‌های iterable با یک سینتکس خاص برای تولید مقادیر بودند؛ درواقع اما generatorها بسیار قدرتمندتر و انعطاف پذیرتر هستند.
449449

450-
That's because `yield` is a two-way street: it not only returns the result to the outside, but also can pass the value inside the generator.
450+
چون `yield` یک خیابان دو طرفه است: نه تنها مقدار را به بیرون برمی‌گرداند بلکه می‌تواند مقادیر را به داخل generator بیاورد.
451451

452-
To do so, we should call `generator.next(arg)`, with an argument. That argument becomes the result of `yield`.
452+
برای این کار باید `()generator.next` را با یک argument صدا کنیم. این argument تبدیل به مقدار برگردانده شده توسط خود yield درون generator می‌شود.
453453

454-
Let's see an example:
454+
برای مثال:
455455

456456
```js run
457457

458458
function* gen() {
459459

460460
\*!*
461461

462-
// Pass a question to the outer code and wait for an answer
462+
// یک سوال را به کد بیرونی برگردانید و منتظر جواب شوید
463463

464464
let result = yield "2 + 2 = ?"; // (*)
465465

@@ -471,37 +471,37 @@ function* gen() {
471471

472472
let generator = gen();
473473

474-
let question = generator.next().value; // <-- yield returns the value
474+
let question = generator.next().value; // <-- مقدار را بر می‌گرداند yield
475475

476-
generator.next(4); // --> pass the result into the generator
476+
generator.next(4); // --> برمی‌گرداند generator نتیجه را به
477477

478478
```
479479

480480

481481
![](genYield2.svg)
482482

483-
1. The first call `generator.next()` should be always made without an argument (the argument is ignored if passed). It starts the execution and returns the result of the first `yield "2+2=?"`. At this point the generator pauses the execution, while staying on the line `(*)`.
483+
1. نمی‌توان اولین بار که `()generator.next` صدا می‌شود به آن argument داد و در صورت داده شدن، نادید گرفته خواهد شد. پس از صدا شدن متود، اجرای generator شروع می‌شود و مقدار اولین yield را برمی‌گرداند. اکنون اجرای generator متوقف شده و در خط `*` مانده است.
484484

485-
2. Then, as shown at the picture above, the result of `yield` gets into the `question` variable in the calling code.
485+
2. سپس مانند تصویر بالا، نتیجه yield اول در متغیر `question` ذخیره می‌شود.
486486

487-
3. On `generator.next(4)`, the generator resumes, and `4` gets in as the result: `let result = 4`.
487+
3. با اجرای `generator.next(4)`، اجرای generator دوباره شروع می‌‎شود و مقدار متغیر `result` برابر `4` می‌شود.
488488

489-
Please note, the outer code does not have to immediately call `next(4)`. It may take time. That's not a problem: the generator will wait.
489+
توجه داشته باشید که نیاز نیست کد بیرونی فورا `(4)next` را صدا کند؛ اگر طول بکشد، generator صبر خواهد کرد.
490490

491-
For instance:
491+
برای مثال:
492492

493493
```js
494494

495-
// resume the generator after some time
495+
// پس از تاخیری دوباره شروع می‌شود generator اجرای
496496

497497
setTimeout(() => generator.next(4), 1000);
498498

499499
```
500500

501501

502-
As we can see, unlike regular functions, a generator and the calling code can exchange results by passing values in `next/yield`.
502+
همان‌طور که مشاهده می‌شود بر خلاف توابع معمولی، یک generator و کد صدا زننده‌اش می‌توانند با هم مقادیر را از طریق `next/yield` رد و بدل کنند.
503503

504-
To make things more obvious, here's another example, with more calls:
504+
یک مثال دیگر:
505505

506506
```js run
507507

@@ -528,31 +528,31 @@ alert(generator.next(9).done); // true
528528
```
529529

530530

531-
The execution picture:
531+
تصویر اجرا:
532532

533533
![](genYield2-2.svg)
534534

535-
1. The first `.next()` starts the execution... It reaches the first `yield`.
535+
1. اولین `()next`اجرای generator را آغاز می‌کند تا به اولین yield برسد.
536536

537-
2. The result is returned to the outer code.
537+
2. نتیجه به کد بیرونی برگردانده می‌شود.
538538

539-
3. The second `.next(4)` passes `4` back to the generator as the result of the first `yield`, and resumes the execution.
539+
3. صدا زده شدن `next(4)` مقدار `4` را به generator به عنوان نتیجه اولین yield باز می‌گرداند و اجرای generator را دوباره شروع می‌کند.
540540

541-
4. ...It reaches the second `yield`, that becomes the result of the generator call.
541+
4. به yield دوم می‌رسد و مقدار آن نتیجه دومین بار صدا شدن `next` است.
542542

543-
5. The third `next(9)` passes `9` into the generator as the result of the second `yield` and resumes the execution that reaches the end of the function, so `done: true`.
543+
5. صدا زده شدن `next(9)`مقدار `9` را به عنوان نتیجه دومین yield برمی‌گرداند و اجرای generator دوباره شروع می‌شود تا به انتهای تابع، `done:true` برسد.
544544

545-
It's like a "ping-pong" game. Each `next(value)` (excluding the first one) passes a value into the generator, that becomes the result of the current `yield`, and then gets back the result of the next `yield`.
545+
درست مثل بازی پینگ پنگ؛ `(value)next` یک مقدار را به generator پاس می‌دهد که نتیجه yield فعلی می‌شود و سپس نتیجه yield بعدی به بیرون پاس داده می‌شود.
546546

547547
## generator.throw
548548

549-
As we observed in the examples above, the outer code may pass a value into the generator, as the result of `yield`.
549+
همان‌طور که در مثال‌های بالا دیدیم، کد بیرونی می‌تواند یک مقدار را به generator در جواب yield پاس بدهد.
550550

551-
...But it can also initiate (throw) an error there. That's natural, as an error is a kind of result.
551+
...اما می‌تواند در آن حین یک ارور پرتاب کند که طبیعی است؛ چون ارور نیز یک جور نتیجه است.
552552

553-
To pass an error into a `yield`, we should call `generator.throw(err)`. In that case, the `err` is thrown in the line with that `yield`.
553+
برای اینکه یک ارور را به yield پاس بدهیم، باید `(err)genrator.throw` را صدا کنیم. در این صورت، `err` در خط با yield پرتاب می‌شود.
554554

555-
For instance, here the yield of `"2 + 2 = ?"` leads to an error:
555+
برای مثال اینجا yield شدن "2 + 2 = ?" باعث ارور می‌شود:
556556

557557
```js run
558558

@@ -562,11 +562,11 @@ function* gen() {
562562

563563
let result = yield "2 + 2 = ?"; // (1)
564564

565-
alert("The execution does not reach here, because the exception is thrown above");
565+
alert("اجرا به اینجا نمی‌رسد چون خط بالا ارور پرتاب کرده است");
566566

567567
} catch(e) {
568568

569-
alert(e); // shows the error
569+
alert(e); // ارور را نشان می‌دهد
570570

571571
}
572572

@@ -578,24 +578,24 @@ let question = generator.next().value;
578578

579579
\*!*
580580

581-
generator.throw(new Error("The answer is not found in my database")); // (2)
581+
generator.throw(new Error("پاسخ در دیتابیس من نیست")); // (2)
582582

583583
\*/!*
584584

585585
```
586586

587587

588-
The error, thrown into the generator at line `(2)` leads to an exception in line `(1)` with `yield`. In the example above, `try..catch` catches it and shows it.
588+
ارور پرتاب شده به داخل generator در خط `2` باعث exception در خط `1` دارای yield می‌شود که در مثال بالا توسط `try..catch` گرفته شده و نمایش داده می‌شود.
589589

590-
If we don't catch it, then just like any exception, it "falls out" the generator into the calling code.
590+
اگر آن را catch نکنیم، مانند هر exception دیگری اجرا از generator به کد بیرونی منتقل می‌شود.
591591

592-
The current line of the calling code is the line with `generator.throw`, labelled as `(2)`. So we can catch it here, like this:
592+
خط فعلی کد صدا زننده، خط دارای `generator.throw` با لیبل `2` است. پس خطا را این گونه هم می‌توان گرفت:
593593

594594
```js run
595595

596596
function* generate() {
597597

598-
let result = yield "2 + 2 = ?"; // Error in this line
598+
let result = yield "2 + 2 = ?"; // خطا در این خط
599599

600600
}
601601

@@ -607,11 +607,11 @@ let question = generator.next().value;
607607

608608
try {
609609

610-
generator.throw(new Error("The answer is not found in my database"));
610+
generator.throw(new Error("پاسخ در دیتابیس من نیست"));
611611

612612
} catch(e) {
613613

614-
alert(e); // shows the error
614+
alert(e); // ارور را نمایش می‌دهد
615615

616616
}
617617

@@ -620,11 +620,11 @@ try {
620620
```
621621

622622

623-
If we don't catch the error there, then, as usual, it falls through to the outer calling code (if any) and, if uncaught, kills the script.
623+
اگر ارور را catch نکنیم، در صورت وجود کد بیرونی اجرا به آن منتقل می‌شود و اگر آن‌جا نیز هندل نشده باشد، اجرای کد با خطا پایان می‌پذیرد.
624624

625625
## generator.return
626626

627-
`generator.return(value)` finishes the generator execution and return the given `value`.
627+
این متود اجرای generator را به اتمام می‌رساند و مقدار argument را به عنوان نتیجه برمی‌گرداند.
628628

629629
```js
630630

@@ -649,20 +649,20 @@ g.next(); // { value: undefined, done: true }
649649
```
650650

651651

652-
If we again use `generator.return()` in a completed generator, it will return that value again ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/return)).
652+
اگر در یک generatorخاتمه یافته دوباره از `()generator.return`استفاده کنیم، همان مقدار را دوباره برمی‌گرداند.([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/return)).
653653

654-
Often we don't use it, as most of time we want to get all returning values, but it can be useful when we want to stop generator in a specific condition.
654+
معمولا از آن استفاده نمی‌شود؛ چون اکثر زمان‌ها می‌خواهیم تمام مقادیر را بگیریم، اما موقعی که بخواهیم در شرایط خاص generator را متوقف کنیم کاربرد دارد.
655655

656-
## Summary
656+
## خلاصه
657657

658-
- Generators are created by generator functions `function* f(…) {…}`.
658+
- generatorها توسط تابع generator تولید می‌شوند. `{...} (...) function* f`
659659

660-
- Inside generators (only) there exists a `yield` operator.
660+
- عملگر yield (فقط) در داخل generatorها وجود دارد.
661661

662-
- The outer code and the generator may exchange results via `next/yield` calls.
662+
- کد بیرونی و generator ممکن است توسط `next/yield` با هم نتایج را رد و بدل کنند.
663663

664-
In modern JavaScript, generators are rarely used. But sometimes they come in handy, because the ability of a function to exchange data with the calling code during the execution is quite unique. And, surely, they are great for making iterable objects.
664+
در جاوااسکریپت مدرن، generatorها کم استفاده می‌شوند. اما گاهی اوقات می‌توانند مفید باشند؛ رد و بدل کردن داده با کد صدا زننده، یک قابلیت منحصر بفرد است. هم‌چنین برای ساخت iterableها هم بکار می‌روند.
665665

666-
Also, in the next chapter we'll learn async generators, which are used to read streams of asynchronously generated data (e.g paginated fetches over a network) in `for await ... of` loops.
666+
علاوه بر آن، در چپتر بعدی، async generatorها را یاد خواهیم گرفت که برای خواندن جریان‌های asynchronously generated data استفاده می‌شود؛ مثلا fetchهای paginatedشده در شبکه توسط `for await...of loop`.
667667

668-
In web-programming we often work with streamed data, so that's another very important use case.
668+
از آنجایی که در برنامه نویسی وب، با جریان‌های داده، زیاد سر و کار داریم این یک کاربرد بسیار مهم است.

0 commit comments

Comments
 (0)