<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by 毒毒 on Medium]]></title>
        <description><![CDATA[Stories by 毒毒 on Medium]]></description>
        <link>https://medium.com/@xydudu?source=rss-4712827891c2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*CvoSkT_f8Qa1dv6g.jpg</url>
            <title>Stories by 毒毒 on Medium</title>
            <link>https://medium.com/@xydudu?source=rss-4712827891c2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 06:26:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@xydudu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[住的小区地下停车场，开出开进都需经过一段单行道，其实道路本是能容两车并行交汇，只是因车位紧张，找不到车位便在这路边停了一排。早送孩子上学以及上班，开出时迎面开进来一辆车，面对着停下，仿佛僵持了一会，最…]]></title>
            <link>https://medium.com/@xydudu/%E4%BD%8F%E7%9A%84%E5%B0%8F%E5%8C%BA%E5%9C%B0%E4%B8%8B%E5%81%9C%E8%BD%A6%E5%9C%BA-%E5%BC%80%E5%87%BA%E5%BC%80%E8%BF%9B%E9%83%BD%E9%9C%80%E7%BB%8F%E8%BF%87%E4%B8%80%E6%AE%B5%E5%8D%95%E8%A1%8C%E9%81%93-%E5%85%B6%E5%AE%9E%E9%81%93%E8%B7%AF%E6%9C%AC%E6%98%AF%E8%83%BD%E5%AE%B9%E4%B8%A4%E8%BD%A6%E5%B9%B6%E8%A1%8C%E4%BA%A4%E6%B1%87-%E5%8F%AA%E6%98%AF%E5%9B%A0%E8%BD%A6%E4%BD%8D%E7%B4%A7%E5%BC%A0-%E6%89%BE%E4%B8%8D%E5%88%B0%E8%BD%A6%E4%BD%8D%E4%BE%BF%E5%9C%A8%E8%BF%99%E8%B7%AF%E8%BE%B9%E5%81%9C%E4%BA%86%E4%B8%80%E6%8E%92-%E6%97%A9%E9%80%81%E5%AD%A9%E5%AD%90%E4%B8%8A%E5%AD%A6%E4%BB%A5%E5%8F%8A%E4%B8%8A%E7%8F%AD-%E5%BC%80%E5%87%BA%E6%97%B6%E8%BF%8E%E9%9D%A2%E5%BC%80%E8%BF%9B%E6%9D%A5%E4%B8%80%E8%BE%86%E8%BD%A6-%E9%9D%A2%E5%AF%B9%E7%9D%80%E5%81%9C%E4%B8%8B-%E4%BB%BF%E4%BD%9B%E5%83%B5%E6%8C%81%E4%BA%86%E4%B8%80%E4%BC%9A-%E6%9C%80-1d38c8352714?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d38c8352714</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Mon, 26 Jun 2023 00:52:27 GMT</pubDate>
            <atom:updated>2023-06-26T00:52:27.596Z</atom:updated>
            <content:encoded><![CDATA[<h3>要豁达点啊</h3><p>住的小区地下停车场，开出开进都需经过一段单行道，其实道路本是能容两车并行交汇，只是因车位紧张，找不到车位便在这路边停了一排。早送孩子上学以及上班，开出时迎面开进来一辆车，面对着停下，仿佛僵持了一会，最后我开到一拐角让了过去。理应也是我让，毕竟拐角在我的这一边，但僵持显然是心有不舒服。这没来由的脾气，我是怪路边一排本不应该停的车，还是对面的车不应等着我让路，仔细想来不至于。只是太不豁达了。</p><p>这段时间甚至更长的时间里，与友闲聊常陷入消极，结束聊天竟是有种徒增烦恼的焦燥，倒还不如自己独处罢了。消极来源离不开两点：工作与生活，总结来说就是怎么活。现在看起来自己活得是比较不快乐，要不然怎么能够攒下能持续这么长时间的聊资。这聊来聊去，围绕着也只是工作上觉得不公平、安排下来的事项觉得不合理以及自认为莫奇妙的公司管理风格。概括起来抱怨便是没有生活的人生吧，有种自己工作之外的生活是轻松愉快的假象。如果自己格调能高一点，看到整个世界，苦难的人那么多，是否能豁达些呢？显然要做到是很难的，这会是一种假惺惺的高格调。</p><p>还是要快乐些，从跟朋友少聊些消极内容开始。如果做不到，索性我就多玩些游戏，我想这总归会是件高兴的事。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d38c8352714" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[失眠两月]]></title>
            <link>https://medium.com/@xydudu/%E5%A4%B1%E7%9C%A0%E4%B8%A4%E6%9C%88-e8b744226826?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8b744226826</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Tue, 14 May 2019 08:23:33 GMT</pubDate>
            <atom:updated>2019-05-14T08:23:33.141Z</atom:updated>
            <content:encoded><![CDATA[<p>完整失眠两月有余，其间靠安眠药勉强有些天入睡3到5个小时。去医院两次，一次直接开助眠类安眠药，花费三十余元；第二次去的长沙唯一一家有睡眠障碍科室的湘雅附二，带着期望面诊，去不想医生只问了三五问题，便结论我为抑郁症，不理我辩解，匆匆便开了超过三百元的药，且全为抗抑郁类药品。本着对医生专业认可，把药买回家来，第一天吃下没睡着，吃不到一周副作用便汹涌袭来，周末吐到腹无一物且头晕如醉酒。</p><p>决定停药，硬抗失眠。连续失眠又一周后，一周末硬撑到凌晨不上床，最后倒床居然睡着。难得一个深度睡眠，虽然时间很短，但精神焕发一新。到今天，能睡着的天数已多过失眠日子，只有一问题遗留就是醒得太早，每日6点醒便无法再入睡且无论前一天何时上床。</p><p>这么长时间的失眠，第一次有此体验，前两月非常焦恼，将投医作为解救唯一。终于放弃吃药，坚决面对时却成功对抗，每一天睡着，就更增多一天的心安，我想终是能彻底回到这段失眠前的状态。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8b744226826" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[脱发]]></title>
            <link>https://medium.com/@xydudu/%E8%84%B1%E5%8F%91-45dca050085e?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/45dca050085e</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Thu, 24 May 2018 09:40:13 GMT</pubDate>
            <atom:updated>2018-05-24T09:40:13.495Z</atom:updated>
            <content:encoded><![CDATA[<p>刚毕业工作，在南京，合租的一位叫晶哥，湖北人，故乡邻近至此我与他多了几分亲切感。晶哥大我七八岁，倒也年轻，却愁着头发掉得厉害，恐怕要谢顶。便见着他每晚拿着带按摩功能的梳子，得空就这么梳着，这事我经常笑话。</p><p>如今我三十也余二，也不知哪年起，感觉头发越来越细，风吹过来，额头就显得白花一大块，终于我也到了担心脱发问题时。想起许多年前晶哥那固定的梳头动作，我不禁失笑。</p><p>最终我也没买一把属于自己的梳子，但一直有再去一次南京，看看那里的老友们，以及看一眼晶哥，是否头发还浓密？带着我这已经不浓密的头。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45dca050085e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[做慈父]]></title>
            <link>https://medium.com/@xydudu/%E5%81%9A%E6%85%88%E7%88%B6-54c55888583c?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/54c55888583c</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Mon, 15 Jan 2018 02:53:45 GMT</pubDate>
            <atom:updated>2018-01-15T02:53:45.097Z</atom:updated>
            <content:encoded><![CDATA[<p>小儿一两岁间，正可爱，衬着大儿卡爷居然像个大孩子，显得不甚可爱。终有些脾气，捡着卡爷就发作，冷静想来，不公平。也只不到5岁的孩童，童稚正当，何来不可爱呢，如何不会发些娇作与弟来争宠。是我犯了错，耐性都给了弟弟，脾气却给的哥哥，应该检讨后，带上卡爷一起胡闹。</p><p>入冬来，早起送卡爷上幼儿园是天大的难事。我最厌早起，尤其天寒时节。迫着自己离了床，就已郁气满满。常扯着卡爷便胡乱的给他整理穿衣，没有好脸色。这也是不对的。</p><p>谨记少发脾气，孩子天真的世界里，需要阳光，慈父。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54c55888583c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[生日]]></title>
            <link>https://medium.com/@xydudu/%E7%94%9F%E6%97%A5-f0a54b04c5ca?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0a54b04c5ca</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Mon, 18 Dec 2017 03:46:52 GMT</pubDate>
            <atom:updated>2017-12-18T03:46:52.451Z</atom:updated>
            <content:encoded><![CDATA[<p>快生日了，顶多只两天。却不高兴，因世界变得不那么可爱，中年人的世界。前些日有一新闻，某一42岁家庭支柱，抛了妻儿父母，居然就跳下几十层楼去，而只是被辞退了。我并不知道他当时多少伤心绝望，只这新闻展示的现实就刺激着我本就也焦虑的心情。同样的我独自撑着家庭，老婆和两小儿，越享受着天伦越无法不焦虑。是现代社会太残酷或是物竞天择，本就是生物体系所必定存在的。归根儿，看来只是生存不易便能解释得通。所以坚强者、强大者，总会赢下来的，倒是突然觉得上面新闻中绝望跳楼的男人，太过冲动，太不坚强。</p><p>生日在年底，于是常在这个时间点，就顺带把一年间的事，总结一下。看今年，收入倒是不错，却是工资比重倒减小了，显然是个好现象，工资便是全部收入的话，恐怕我的焦虑还要大上几倍。但不好的是，除开工资，其它的收入方式都不稳定，明年要解决好此问题，或者说寻找方法罢。做的差的是自我心情的调节，常陷入低落，直至影响工作。工作不顺利则又让心情更低落，这就成解不开的套，结成死结。是一直想找个心理医生，但拖着又没行动，就像我想去医院把我脸上的痣去掉一样，总是没成行，就成了可能再也不会去做的事了。</p><p>就说些不相干的吧，我生日日期相关的居然有三个日子，1103是身份证上写明着的，而依着的实际是农历1103。而当年农历1103对应着公历1214。网络上我有时留着生日1103，有时1214，各网站到日子发着提醒都两次了，谁想着，嘿，我生日是两天后呢。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0a54b04c5ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[电梯]]></title>
            <link>https://medium.com/@xydudu/%E7%94%B5%E6%A2%AF-1f90e5459796?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f90e5459796</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Tue, 21 Nov 2017 11:23:50 GMT</pubDate>
            <atom:updated>2017-11-21T12:52:18.318Z</atom:updated>
            <content:encoded><![CDATA[<p>我家在三楼，工作地更只是二楼，便没觉电梯重要。忽然办公室搬迁，5层的老楼，无电梯，我工位恰是在5楼。来回几天后，腿脚居然累了，感叹这平常事物竟已对人影响颇深了。</p><p>我出身穷苦地，大约初中才知电梯是何物。现今想起，还是有趣。那是去小城参加会议，住宿安排在一宾馆中，我傻而羞着脸，揣着房卡便拐上楼梯往目标楼层爬，喘着气上去发现伙伴们竟已在了。才知电梯存在，羞着更红的脸，拉了一伙伴，钻进电梯厢，便上上下下没停顿的反复好些趟，出来有种满足，但生理感受不好，快是要吐了。</p><p>往后升学在小城里读书，见的机会便多了，倒没继续有可说的趣事。但却也忘不了带着爷爷奶奶第一次体验电梯的情景，两位老人把颤抖的脚小步的挪进厢体内，紧张神情持续全程。出来长呼口气，居然感叹还是共产党好，甚是可爱。不想时间过得飞快，爷爷已然仙去，奶奶虽不至再过苦日子，但寂寞孤独，我们这样世俗间挣扎的人儿，总归难让老人满意，只望假日得闲，能小陪几日，表心意一二。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f90e5459796" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[阴雨的初冬]]></title>
            <link>https://medium.com/@xydudu/%E9%98%B4%E9%9B%A8%E7%9A%84%E5%88%9D%E5%86%AC-3c96b996b453?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c96b996b453</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Fri, 17 Nov 2017 06:49:58 GMT</pubDate>
            <atom:updated>2017-11-17T06:49:58.479Z</atom:updated>
            <content:encoded><![CDATA[<p>阴雨的初冬，恼人的有雾霾，恰老罗一场漫长发布会，倒合我意，便没思量的第一时间购入他家的空气净化器。快递到家时，却吓一跳，巨大的家伙，盒子装的精致，七手八脚拆开来时，玉白的机器透着让我满足的气质。也没说要如何深究这机器功效何如，只且相信罢了，孩子们在家能有一方干净，大家其乐融融。</p><p>工作事没大变化，不满意但也无力，精力常被无聊事耽着，满是无奈。就说些不相干的琐事吧，新的停车场开放了，环境甚好，往地下竟有三层，怕是能将整广电的车装进去。这喜悦跟要每月200费用放一起，心就平静起来，嗨，只是个平常事。这时间，收拾桌上什物，打包进箱子，箱面书写上名字及箱子去处，得点闲儿便写点字。是啊，这是又要换地儿办公了，沉着眼回忆，这已是我第9次换办公点。不可理解哦！公司上月有新通知，有了个新规矩，上班时间够上10小时才符合考勤要求。8小时工作，2小时供打瞌睡，倒是能理解这规矩的由头，因着员工们都有着午间饭后小睡的习惯，按着8小时，怕是工作时间不能够。总之这规矩定下后，怨言风起却也无大浪，这余一月总归是按着要求出勤，不愿意的心情且同事间家常里短就消解不少。</p><p>年底事事需总结，人人等着一些结果或是作出些决定。我也脱不了这俗，明年各种景观时常走马灯般在寂静时进入眼睛。看能出来个什么样的未来，不至于让家人担心。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c96b996b453" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[三月南方在下雨]]></title>
            <link>https://medium.com/@xydudu/%E4%B8%89%E6%9C%88%E5%8D%97%E6%96%B9%E5%9C%A8%E4%B8%8B%E9%9B%A8-223aacb11a81?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/223aacb11a81</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Wed, 15 Mar 2017 03:19:09 GMT</pubDate>
            <atom:updated>2017-03-15T03:19:09.045Z</atom:updated>
            <content:encoded><![CDATA[<p>我是悲观的人，常把事情想到坏的方向，然后跟心情形成一个死循环。却又没有一个强制重启的选项，结果只能持续的保持着低迷，像 CPU 已耗尽的电脑，处理任何事情都力不从心了。</p><p>昨晚安静的看完电影《海边的曼切斯特》，居然失眠了，累加这之前广州恒大一场憋屈的球赛，可能这一个晚上太过抑郁。我越来喜欢看这种安静的电影，但却也是这种不自觉中，容易带进那种伤怀。毕竟安静的电影，多数都在讲忧伤的故事。</p><p>哦，还有这三月南方的雨啊，怎么能让我舒展开压紧的心情呢？我的房间在三楼，透过窗就能看见嫩芽在树枝上，花也开始点缀。所以我知道，会有美好的事物将要出现，但依然我无法高兴。</p><p>真是折磨人的小娇儿呢！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=223aacb11a81" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[嘿，我的2016年咩]]></title>
            <link>https://medium.com/@xydudu/%E5%98%BF-%E6%88%91%E7%9A%842016%E5%B9%B4%E5%92%A9-d1328b44564e?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1328b44564e</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Mon, 16 Jan 2017 08:49:54 GMT</pubDate>
            <atom:updated>2017-01-16T08:49:54.515Z</atom:updated>
            <content:encoded><![CDATA[<p>飞快的过完这一年，但也完成了两件大事：生了小儿书默、买了一市区房子。而不管把这两件事放在之前任何一年中，都将是了不起的一年，偏偏2016过完我却没有这样的骄傲。觉得2016有些别扭，工作的烦扰是主因。</p><p>年初辞职闹得个鸡犬升天，最终未能如愿离职，埋下一年颓唐的种子。现在想，当时绝决坚强点，可能会换来活力积极的一年。王家卫说：“说人生无悔，那都是赌气的话；若真无悔，那人生该多没趣啊”。所以，当初的决定，就当给以后人生增加点乐趣吧。</p><p>身体很累，今年彻底接管了大儿子生活起居，虽也有受不了发些莫名火，总算也熬过来，且磨了自己的性子，连开车都心平气和许多。业余生活是没了，连在家写码看书的时间，也是断断续续连不成一条线儿了，索性时常就任了性子用游戏打发了这些碎片时间。稍安慰的是，统计一番这一年竟还读完了25本书。保持看书的习惯，不管工作生活多么压抑荒唐。</p><p>精神很累，心慌的感觉一直作怪。从小我可能就缺乏安全感，父亲工作时常调动，我随时可能切换环境，显然连从小玩到大的朋友都不会有。所以我害怕环境变换，仿佛一切都会随时失去。于是不断的想，要怎么样才能保证当前阻止变化，精神压力随之有点无限增长。这恼人的虚幻啊，我还需要修行。居然连变化可能带来好的一面，也完全忽略了。</p><p>这一年接收的负能量太多，导致我对现实的评估低于实际，自己徒增烦恼。希望2017年能正视自己以及所有。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1328b44564e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What are Generator Functions?]]></title>
            <link>https://medium.com/@xydudu/what-are-generator-functions-82269c344b3d?source=rss-4712827891c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/82269c344b3d</guid>
            <dc:creator><![CDATA[毒毒]]></dc:creator>
            <pubDate>Thu, 03 Nov 2016 08:39:55 GMT</pubDate>
            <atom:updated>2016-11-03T08:39:55.330Z</atom:updated>
            <content:encoded><![CDATA[<p>文章原文：http://thejsguy.com/2016/10/15/a-practical-introduction-to-es6-generator-functions.html</p><p>Generator functions, also called generators for short, are an ES6/ES2015 feature that might seem confusing at first with little practicality. However, once you take time to learn how they work and see some practical examples, you’ll really see how powerful and useful the can be. In this post, we’ll cover the following:</p><ul><li>What are Generator Functions</li><li>How Generator Functions Work</li><li>Generator Functions and Promises</li><li>Generator Examples in the Wild</li></ul><p><em>Generator Functions</em> 是 ES6/ES2015 的新特性，对于初学且没有太多经验者来说，对这个会很迷惑。但是只要花点时间学习，看看实例，然后实践一下，就能体验到它功能强大与实用。 这个文章包括下面几个部分：</p><ul><li>什么是 <em>Generator Functions</em></li><li>它是怎么工作的</li><li>它与 Promises 的比较</li><li>几个例子</li></ul><h4>What are Generator Functions?</h4><p>Functions in JavaScript are known as “run to completion”. When a functin is invoked, the body of the function will execute until it reaches the end. The function can’t be paused for other code to execute.</p><p>大家所理解的 JavaScript 函数，一旦被触发后，函数体所有的代码会被执行直到结束，整个过程不能被外面的函数所控制，如：暂停。</p><p>Generator function however are not “run to completion”. Generator function can be paused and resumed so that other code can execute in between. The great thing about this behavior is that we can use generators to manage flow control. Because generators allow us to pause execution, we can easily cancel aynchronous operations. Generators also allow us to turn asynchronous code into synchronous-looking code.</p><p>而 Generator 则提供了一种方式，在函数运行过程间，对函数进行暂停、恢复执行等的控制。另外通过 Generator 还可以让我们方便的把异步代码转换为同步代码的写法，使代码更且可读性。</p><p>For example, instead of our code looking like this:</p><pre>Artist.findByID(id).then(artist =&gt; {<br>    artist.getSongs().then(songs =&gt; {<br>        console.log(songs)<br>    })<br>})</pre><p>Imagine if our code looked like this instead:</p><pre>let artist = yield Artist.findByID(id)<br>let songs = yield artist.getSongs()<br>console.log(songs)</pre><p>This asynchronous code looks synchronous and is easier to read.</p><p>上面的例子可以看出，异步代码就好像同步代码一样的写法，读起来就很容易懂了。</p><h4>How Generator Function Work</h4><p>A generator function is declared just like a regular function but with an asterisk(星号符*) after the <em>function</em> keword:</p><pre>function *doSomethingAsync() {}<br>// or<br>function* doSomethingAsync() {}<br>// or<br>function*doSomethingAsync() {}</pre><p>These are all functionally the same, just stylistically different. You can also have anonymous generator functions:</p><pre>function*() {}<br>// or<br>function *() {}<br>// or<br>function* () {}</pre><p>上面的代码是介绍一个 generator function 怎么定义，主要就是需要加 * 号符。</p><p>When you invoke a generator function, it won’t execute the body of the function like a regular function. Instead, it will return a generator object called an iterator. Iterators are a topic for another blog post, but in the context of a generator, it is an object that controls the execution of the generator function via a <em>next()</em> method. Let’s look at an example.</p><p>当 generator 被触发后，不会像普通函数一样执行函数本体，而是返回一个叫 interator 的对象，这个 interator 通过 next() 方法来控制 generator 的执行。</p><pre>function * myGenerator() {<br>    console.log(1)<br>    let a = yield &#39;first yield&#39;<br>    console.log(a)<br>    let b = yield &#39;second yield&#39;<br>    console.log(b)<br>    return &#39;hi&#39;<br>}<br>let iterator = myGenerator()<br>let firstYield = interator.next()<br>// {value: &#39;first yield&#39;, done: false}</pre><p>Here, we’ve defined a generator function called <em>myGenerator</em> and we’ve created the iterator object by invoking the generator function. At this point, the body of <em>myGenerator</em> hasn’t started executing. It isn’t until we called <em>interator.next()</em> when the body of <em>myGenerator</em> starts executing. By calling <em>interator.next()</em>, the generator’s body will execute until the first <em>yield</em> statement and then pause. With the code above, we will see <em>1</em> and <em>{value: ‘first yield’, done: false}</em> logged to the console. Calling <em>interator.next()</em> returned an object in the format <em>{value: , done: }</em> . The <em>value</em> property in this object is the value next to the <em>yield</em> statement. The <em>done</em> property is a boolean indicating whether the generator has finished executing or not. Basically the <em>yield</em> statement allows us to send values to the caller of the generator function. Let’s resume the generator function.</p><p>myGenerator 触发后，创建了一个 interator 对象，而 myGenerator 函数体内的代码并没有执行。直到调用 interator.next()，开始执行函数体内的代码，当遇到 yield 时，执行就停止了，等待下一次 next 的触发。同时，interator.next() 会返回一个 json 对象，包括两个字段：value 和 done。value 是 yield 出来的值，done 则是表明 generator 是否执行完成了。</p><pre>function *myGenerator() {<br>    console.log(1)<br>    let a = yield &#39;first yield&#39;<br>    console.log(a)<br>    let b = yield &#39;second yield&#39;<br>    console.log(b)<br>    return &#39;hi&#39;<br>}<br>let interator = myGenerator()<br>let firstYield = interator.next()<br>let secondYield = interator.next(2)<br>let generatorReturnValue = interator.next(3)</pre><p>When we call <em>iterator.next()</em> a second time but pass in <em>2</em> (interator.next(2)), we’ll see that we can pass values back into the genrator function as the result of a <em>yield</em> statement, and thus the variable <em>a</em> gets assigned <em>2</em> . The generator will resume execution until the second <em>yield</em> statement, and then pause again. By calling <em>interator.next(3)</em> , the generator will resume and the variable <em>b</em> in the generator will get assigned <em>3</em>, and the generator will finish executing, resulting in <em>interator.next(3)</em> returning <em>{value: ‘hi’, done: true}</em> . Notice how <em>done</em> is now <em>true</em> , and <em>value</em> is the return value of the genrator function.</p><p>继续加了一些代码，主要解释了 next 运行时会发生什么，以及顺序。而且 interator.next() 是可以传值进去的，比如上面第二次调用 next 时， interator.next(2) ，把 2 当 next 的参数传进去，被赋值给了 a 。 另外，当函数体执行 return 后，代表该 generator 执行完毕了， next 返回的对象中 done 变为 true ， value 则为 return 的值。</p><p>This example illustrates the basics of how generator work. We used the <em>yield</em> keyword to pause the generator function twice and send values to the caller. We can resume the execution of the generator by calling <em>next()</em> on the interator, optionally passing in an argument (except for the first time) which allows us to send data back to the generator as the results of the <em>yield</em> .</p><p>这个简单的例子可以了解到， 在 generator 中，可以使用 yield 来暂停 generator 的执行，使用 next() 来恢复执行。而 next() 还可以传参数进到 genrator 中，当作是 yield 的返回值。（注意第一次的 next() 传进来的参数将被忽略，因为没地方给他赋值咩）</p><h4>Generator Functions and Promises</h4><p>Generator functions and promises are not mutually exclusive. In fact, they work really well together. We can create an abstraction that allows us to yield promises, and only resume the generator function if yielded promises resolve.</p><p>Generator 跟 Promise 并不排斥，这两个东西一起能玩出花来，比如我们像下面这样写异步代码。</p><p>Our end goal is that we want to write some asynchronous code like this:</p><pre>task(function *() {<br>    let artist = yield Artist.findByID(1)<br>    let songs = yield artist.getSongs()<br>    console.log(artist, songs)<br>})</pre><p>This code has 2 asynchronous calls, <em>Artist.findByID()</em> and <em>artist.getSongs()</em>.</p><p>上面代码有两个方法是异步的。</p><p>To enable synchronous-looking code like this, we’ll create a wrapper function called <em>task</em> that will accept a generator function and invoke it. It will keep calling <em>inerator.next()</em> each time a <em>promise</em> is yielded and resolves.</p><pre>function task(generator) {<br>    let iterator = generator()<br>    recursivelyNext()<br>    <br>    function recursivelyNext(data) {<br>        let yield = iterator.next.apply(iterator, arguments)<br>        if (isPromise(yielded.value)) {<br>            yielded.value.then(data =&gt; {<br>                recursivelyNext(data)<br>            })<br>        }<br>    }<br>}<br><br>function isPromise(val) {<br>    return val &amp;&amp; typeof val.then === &#39;function&#39;<br>}</pre><p>上现就是一个简单的 task 方法的实现，可以到 <a href="http://www.es6fiddle.net/iuca4m0l/">http://www.es6fiddle.net/iuca4m0l/</a> 上查看。</p><h4>Generator Examples in the Wild</h4><ol><li>co: <a href="https://github.com/tj/co">https://github.com/tj/co</a></li><li><a href="http://koajs.com/">http://koajs.com/</a></li></ol><h4>Conclusion</h4><p>Generators are functions that are not ‘run to completion’. They are functions that can be paused and resumed, allowing for abstractions to be written so that we can do things like turn asynchronous code into synchronous-looking code and cancel asynchronous operations.</p><p>总结来说，Generator 不是普通的函数，触发执行到结束完成生命。它是一种可以在外部控制其执行、暂停、恢复执行的特殊函数。通过这个特性，可以把用易懂的同步执行思维来写异步执行的代码。</p><p><a href="http://thejsguy.com/2016/10/15/a-practical-introduction-to-es6-generator-functions.html">A Practical Introduction to ES6 Generator Functions - The JS Guy</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82269c344b3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>