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
```smart header="generatorها ممکن است تا ابد مقدار تولید کنند"
321
321
322
-
در مثال بالا، یک دنباله کران دار تولید کردیم، ولی میتوان به همان روش یک دنباله بیکران از مقادیر را ساخت. مثل یک دنباله بیپایان از اعداد شبه تصادفی.
322
+
در مثال بالا، یک دنباله کراندار تولید کردیم، ولی میتوان به همان روش یک دنباله بیکران از مقادیر را ساخت. مثل یک دنباله بیپایان از اعداد شبه تصادفی.
323
323
324
324
```
325
325
@@ -443,23 +443,23 @@ alert(str); // 0..9A..Za..z
443
443
444
444
ترکیب generatorها یک راه معقول برای استفاده از جریان یک generator درون دیگری است و از حافظه بیشتر برای ذخیره مقادیر استفاده نمیکند.
445
445
446
-
## "yield" is a two-way street
446
+
## yield یک خیابان دو طرفه است.
447
447
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ها بسیار قدرتمندتر و انعطاف پذیرتر هستند.
449
449
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 بیاورد.
451
451
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 میشود.
453
453
454
-
Let's see an example:
454
+
برای مثال:
455
455
456
456
```js run
457
457
458
458
function*gen() {
459
459
460
460
\*!*
461
461
462
-
//Pass a question to the outer code and wait for an answer
462
+
//یک سوال را به کد بیرونی برگردانید و منتظر جواب شوید
463
463
464
464
let result =yield"2 + 2 = ?"; // (*)
465
465
@@ -471,37 +471,37 @@ function* gen() {
471
471
472
472
let generator =gen();
473
473
474
-
let question =generator.next().value; // <-- yield returns the value
474
+
let question =generator.next().value; // <-- مقدار را بر میگرداند yield
475
475
476
-
generator.next(4); // --> pass the result into the generator
476
+
generator.next(4); // --> برمیگرداند generator نتیجه را به
477
477
478
478
```
479
479
480
480
481
481

482
482
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 متوقف شده و در خط `*` مانده است.
484
484
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`ذخیره میشود.
486
486
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` میشود.
488
488
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 صبر خواهد کرد.
490
490
491
-
For instance:
491
+
برای مثال:
492
492
493
493
```js
494
494
495
-
//resume the generator after some time
495
+
//پس از تاخیری دوباره شروع میشود generator اجرای
496
496
497
497
setTimeout(() =>generator.next(4), 1000);
498
498
499
499
```
500
500
501
501
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` رد و بدل کنند.
503
503
504
-
To make things more obvious, here's another example, with more calls:
1.The first `.next()` starts the execution... It reaches the first `yield`.
535
+
1.اولین `()next`اجرای generator را آغاز میکند تا به اولین yield برسد.
536
536
537
-
2.The result is returned to the outer code.
537
+
2.نتیجه به کد بیرونی برگردانده میشود.
538
538
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 را دوباره شروع میکند.
540
540
541
-
4....It reaches the second `yield`, that becomes the result of the generator call.
541
+
4.به yield دوم میرسد و مقدار آن نتیجه دومین بار صدا شدن `next` است.
542
542
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` برسد.
544
544
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 بعدی به بیرون پاس داده میشود.
546
546
547
547
## generator.throw
548
548
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 پاس بدهد.
550
550
551
-
...But it can also initiate (throw) an error there. That's natural, as an error is a kind of result.
551
+
...اما میتواند در آن حین یک ارور پرتاب کند که طبیعی است؛ چون ارور نیز یک جور نتیجه است.
552
552
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 پرتاب میشود.
554
554
555
-
For instance, here the yield of `"2 + 2 = ?"` leads to an error:
555
+
برای مثال اینجا yield شدن "2 + 2 = ?" باعث ارور میشود:
556
556
557
557
```js run
558
558
@@ -562,11 +562,11 @@ function* gen() {
562
562
563
563
let result =yield"2 + 2 = ?"; // (1)
564
564
565
-
alert("The execution does not reach here, because the exception is thrown above");
565
+
alert("اجرا به اینجا نمیرسد چون خط بالا ارور پرتاب کرده است");
566
566
567
567
} catch(e) {
568
568
569
-
alert(e); //shows the error
569
+
alert(e); //ارور را نشان میدهد
570
570
571
571
}
572
572
@@ -578,24 +578,24 @@ let question = generator.next().value;
578
578
579
579
\*!*
580
580
581
-
generator.throw(newError("The answer is not found in my database")); // (2)
581
+
generator.throw(newError("پاسخ در دیتابیس من نیست")); // (2)
582
582
583
583
\*/!*
584
584
585
585
```
586
586
587
587
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`گرفته شده و نمایش داده میشود.
589
589
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 به کد بیرونی منتقل میشود.
591
591
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` است. پس خطا را این گونه هم میتوان گرفت:
593
593
594
594
```js run
595
595
596
596
function*generate() {
597
597
598
-
let result =yield"2 + 2 = ?"; //Error in this line
598
+
let result =yield"2 + 2 = ?"; //خطا در این خط
599
599
600
600
}
601
601
@@ -607,11 +607,11 @@ let question = generator.next().value;
607
607
608
608
try {
609
609
610
-
generator.throw(newError("The answer is not found in my database"));
610
+
generator.throw(newError("پاسخ در دیتابیس من نیست"));
611
611
612
612
} catch(e) {
613
613
614
-
alert(e); //shows the error
614
+
alert(e); //ارور را نمایش میدهد
615
615
616
616
}
617
617
@@ -620,11 +620,11 @@ try {
620
620
```
621
621
622
622
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 نکنیم، در صورت وجود کد بیرونی اجرا به آن منتقل میشود و اگر آنجا نیز هندل نشده باشد، اجرای کد با خطا پایان میپذیرد.
624
624
625
625
## generator.return
626
626
627
-
`generator.return(value)` finishes the generator execution and return the given `value`.
627
+
این متود اجرای generator را به اتمام میرساند و مقدار argument را به عنوان نتیجه برمیگرداند.
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)).
653
653
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 را متوقف کنیم کاربرد دارد.
655
655
656
-
## Summary
656
+
## خلاصه
657
657
658
-
-Generators are created by generator functions `function* f(…) {…}`.
658
+
-generatorها توسط تابع generator تولید میشوند. `{...} (...) function* f`
659
659
660
-
-Inside generators (only) there exists a `yield` operator.
660
+
-عملگر yield (فقط) در داخل generatorها وجود دارد.
661
661
662
-
-The outer code and the generator may exchange results via `next/yield`calls.
662
+
-کد بیرونی و generator ممکن است توسط `next/yield`با هم نتایج را رد و بدل کنند.
663
663
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ها هم بکار میروند.
665
665
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`.
667
667
668
-
In web-programming we often work with streamed data, so that's another very important use case.
668
+
از آنجایی که در برنامه نویسی وب، با جریانهای داده، زیاد سر و کار داریم این یک کاربرد بسیار مهم است.
0 commit comments