From 8d39e0c18d7a1677c45d79613128e76dbc56b74e Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 28 Oct 2019 21:31:17 +0600 Subject: [PATCH] generators --- JavaScript_Advance/generators.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 JavaScript_Advance/generators.js diff --git a/JavaScript_Advance/generators.js b/JavaScript_Advance/generators.js new file mode 100644 index 0000000..c8fedb1 --- /dev/null +++ b/JavaScript_Advance/generators.js @@ -0,0 +1,30 @@ +/* + Generators are functions with the possibility of exit and subsequent entry. + Their execution context (variable values) is preserved on subsequent inputs. +*/ + +// Let's consider a simple example: +function* myGenerator() { + yield 5 + yield 6 +} + +// In the example above, we wrote a simple generator function with asteriks(*) notation. +// Next to the yield we put values that are going to be extracted from the function +// In order to extract them one by one, we should at first call myGenerator function + +const gen = myGenerator() + +// The returning value of myGenerator function is a object-iterator. It has next() method +// Which we may call to get the current generator function value: + +gen.next().value // 5 + +// We get the value field of 'next' method's returning value, wich is an object as well +// Let's call this again in order to get the next value: + +gen.next().value // 6 + +// After we iterate through all the values, the next value is going to be undefined. + +gen.next().value // undefined.