{"id":7641,"date":"2019-11-13T22:44:20","date_gmt":"2019-11-13T22:44:20","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7641"},"modified":"2019-11-14T08:51:51","modified_gmt":"2019-11-14T08:51:51","slug":"javascript-generator-functions","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/javascript-generator-functions\/","title":{"rendered":"JavaScript generator functions"},"content":{"rendered":"<p>In C# we have iterators which might return data, such as a collection but can also yield return values allowing us to more dynamically return iterable data. <\/p>\n<p>Yield basically means return a value but store the position within the iterator so that when the iterator is called again, execution starts at the next command in the iterators.<\/p>\n<p>These same type of operations are available in JavaScript using generator functions. Let&#8217;s look at a simple example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfunction* generator() {\r\n  yield &quot;A&quot;;\r\n  yield &quot;B&quot;;\r\n  yield &quot;C&quot;;\r\n  yield &quot;D&quot;;\r\n}\r\n<\/pre>\n<p>The <strong>function*<\/strong> denotes a generator function and it&#8217;s only in the generator function we can use the yield keyword.<\/p>\n<p>When this function is called a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Generator\" rel=\"noopener noreferrer\" target=\"_blank\">Generator<\/a> object is returned which adheres to the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Iteration_protocols#The_iterable_protocol\" rel=\"noopener noreferrer\" target=\"_blank\">iterable protocol<\/a> and the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Iteration_protocols#The_iterator_protocol\" rel=\"noopener noreferrer\" target=\"_blank\">iterator protocol<\/a> which basically means, the Generator acts like an iterator.<\/p>\n<p>Hence executing the following code will result in each yield value being output, i.e. A, B, C, D<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfor(const element of generator()) {\r\n  console.log(element);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In C# we have iterators which might return data, such as a collection but can also yield return values allowing us to more dynamically return iterable data. Yield basically means return a value but store the position within the iterator so that when the iterator is called again, execution starts at the next command in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45],"tags":[],"class_list":["post-7641","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7641"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7641\/revisions"}],"predecessor-version":[{"id":7656,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7641\/revisions\/7656"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}