{"id":1631,"date":"2018-11-19T15:44:43","date_gmt":"2018-11-19T14:44:43","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=1631"},"modified":"2021-02-10T16:24:10","modified_gmt":"2021-02-10T15:24:10","slug":"es6-async-generators","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/es6-async-generators\/","title":{"rendered":"ES6 async\/generators"},"content":{"rendered":"\n<p>Source:&nbsp;<a href=\"https:\/\/www.promisejs.org\/generators\/\">https:\/\/www.promisejs.org\/generators\/<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Example usage\nvar login = async(function* (username, password, session) {\n  var user = yield getUser(username);\n  var hash = yield crypto.hashAsync(password + user.salt);\n  if (user.hash !== hash) {\n    throw new Error('Incorrect password');\n  }\n  session.setUser(user);\n});\n\n\/\/ async implementation\nfunction async(makeGenerator){\n  return function () {\n    var generator = makeGenerator.apply(this, arguments);\n\n    function handle(result){\n      \/\/ result => { done: [Boolean], value: [Object] }\n      if (result.done) return Promise.resolve(result.value);\n\n      return Promise.resolve(result.value).then(function (res){\n        return handle(generator.next(res));\n      }, function (err){\n        return handle(generator.throw(err));\n      });\n    }\n\n    try {\n      return handle(generator.next());\n    } catch (ex) {\n      return Promise.reject(ex);\n    }\n  }\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Convert array to generator\nfunction* iterator(iterable) {\n\tfor(const item of iterable)\n\t\tyield item;\n}\n\/\/ Convert generator (iterator) to array\nArray.from(iterator([1,2,3]));\n<\/pre>\n\n\n\n<p>Lazy evaluation<\/p>\n\n\n\n<p>Source: <a href=\"https:\/\/codepen.io\/jasonmcaffee\/pen\/ZEpeQBq?editors=0010\">https:\/\/codepen.io\/jasonmcaffee\/pen\/ZEpeQBq?editors=0010<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(_ => {\nasync function main(){\n  const l = lazy([1, 2, 3, 5, 6, 7, 8])\n    .filter(x => x % 2 === 0)\n    .map(x => x * x)\n    .filter(x => x === 4)\n    .map(x => x + 1)\n    .take(1);\n  \n  for(let x of l){\n    console.log(`x: `, x);\n  } \n}\n\nfunction* take(iter, count){\n  for (const x of iter){\n    if(count-- &lt;= 0){ return; }\n    yield x;\n  }\n}\n\nfunction* map(iter, func){\n  for (const x of iter){\n    console.log(`map called`);\n    yield func(x);\n  }\n}\n\nfunction* filter(iter, filterFunc){\n  for (const x of iter){\n    if(filterFunc(x)){\n      console.log(`filter called`);\n      yield x;\n    }\n  }\n}\n\nfunction* allOf(iter){\n  for(let x of iter){\n    yield x;\n  }\n}\n\nfunction lazy(arr){\n  const api = {\n    previousGenerator: allOf(arr),\n    map(mapFunc){\n      this.previousGenerator = map(this.previousGenerator, mapFunc);\n      return this;\n    },\n    filter(filterFunc){\n      this.previousGenerator = filter(this.previousGenerator, filterFunc);\n      return this;\n    },\n    take(count){\n      this.previousGenerator = take(this.previousGenerator, count);\n      return this;\n    },\n    collect(){\n      return [...this.previousGenerator];\n    },\n    [Symbol.iterator]() { \n      return this.previousGenerator;\n    },\n  };\n  return api;\n}\n\nmain();\n})();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Source:&nbsp;https:\/\/www.promisejs.org\/generators\/ Lazy evaluation Source: https:\/\/codepen.io\/jasonmcaffee\/pen\/ZEpeQBq?editors=0010<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1631","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=1631"}],"version-history":[{"count":7,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1631\/revisions"}],"predecessor-version":[{"id":4620,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1631\/revisions\/4620"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=1631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=1631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=1631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}