ã¢ã³ãã¬ã€ã»ãµãããã³ïŒ filipovskii_off ïŒ

ä»æ¥ãæ¯æ¥æ°ããããã°ã©ãã³ã°èšèªïŒGoãRustãCoffeeScriptïŒããããŸãã ç§ãèªåã®ããã°ã©ãã³ã°èšèªãèãåºããããšæ±ºããŸãããäžçã«ã¯æ°ããèšèªãæ¬ ããŠããŸãã...
ãååžã®çæ§ãä»æ¥ã¯SchlechtïŒã¹ã¯ãªãããã玹ä»ããŸãïŒã¹ã¯ãªããã¯ã¯ã¬ã€ãžãŒãªããã°ã©ãã³ã°èšèªã§ãã ããã«äœ¿çšãéå§ããå¿ èŠããããŸãã ããã¯ç§ãã¡ãæ £ããŠãããã¹ãŠã®ãã®ãæã£ãŠããŸã-ããã¯æ¡ä»¶æŒç®åãæã¡ããµã€ã¯ã«ããããé¢æ°ãšé«æ¬¡ã®é¢æ°ããããŸãã äžè¬çã«ãéåžžã®ããã°ã©ãã³ã°èšèªã«å¿ èŠãªãã®ã¯ãã¹ãŠæã£ãŠããŸãã
ããŸãäžè¬çã§ã¯ãªããäžèŠããã ãã§æŒãã®ããããããšããããŸãããSchlechtïŒã¹ã¯ãªããé¢æ°ã«ã¯è²ãä»ããŠããŸãã

ã€ãŸããé¢æ°ã宣èšãããšããåŒã³åºããšãã«ããã®è²ãæ瀺çã«æå®ããŸãã
æ©èœã¯ãèµ€ãšéã®2è²ã§ãã
éèŠãªãã€ã³ãïŒéãé¢æ°å ã§ã¯ãä»ã®éãé¢æ°ã®ã¿ãåŒã³åºãããšãã§ããŸãã éãé¢æ°å ã§èµ€ãé¢æ°ãåŒã³åºãããšã¯ã§ããŸããã

èµ€ã®é¢æ°å ã§ã¯ãèµ€ãšéã®äž¡æ¹ã®é¢æ°ãåŒã³åºãããšãã§ããŸãã

ç§ã¯ãããã¹ãã ãšæ±ºããŸããã ãã¹ãŠã®èšèªã¯ãã®ããã§ãªããã°ãªããŸããã
埮åŠãªç¹ïŒèµ€ãé¢æ°ãæžããŠåŒã³åºãããšã¯çãã§ãïŒ ãçãããšèšããšããã©ãããæå³ã§ããïŒ å®éãç§ã¯ä»ãã€ãèªãå匷ããŠããã®ã§ãèµ€ãé¢æ°ã¯ãã¹ãŠãã€ãèªã§åŒã³åºãã¹ãã ãšæ±ºããŸããã

ããã¯ãã€ãèªã§é¢æ°ãæžãæ¹æ³ã§ãïŒ

ãïŒãã¯å¿ é ã§ã-çµå±ããã€ãèªã§æžããŠããŸãã
ãã®ãããªèšèªã§æžãã«ã¯ïŒ 2ã€ã®æ¹æ³ããããŸãã éè²ã®é¢æ°ã®ã¿ã䜿çšã§ããŸããããã®äžã«æžã蟌ãã®ã¯èŠçã§ã¯ãããŸããããå éšã§ã¯èµ€è²ã®é¢æ°ã¯äœ¿çšã§ããŸããã ãã®ã¢ãããŒãã¯ããŸããããŸãããã€ã³ã¹ãã¬ãŒã·ã§ã³ãåŸãŠãèµ€ãé¢æ°ã«é¢ããæšæºã©ã€ãã©ãªã®ååãæžããã®ã§ããã¿ãŸãã...
ããªããžã®è³ªå-ãã®ãããªèšèªã䜿çšããŸããïŒ SchlechtïŒScriptã販売ããŸãããïŒ
ãŸããããªãã¯ãããã°ãéžæã®äœå°ããããŸããã ããããªãã...
Javascript
JavaScriptã¯çŽ æŽãããèšèªã§ããç§ãã¡ã¯çããããæããŠããŸããJavaScriptã倧奜ããªã®ã§ãç§ãã¡ã¯çããã«ããŸãã ããããåé¡ã¯JavaScriptãSchlechtïŒScriptã®æ©èœã®äžéšãç¶æ¿ããŠããããšã§ãããèªæ ¢ããããããŸããããç§ã®ã¢ã€ãã¢ãããã€ãçãã ãšæããŸãã
圌ãã¯æ£ç¢ºã«äœãç¶æ¿ããŸããïŒ JavaScriptã«ã¯èµ€ãšéã®æ©èœããããŸãã JavaScriptã®èµ€ã®é¢æ°ã¯éåæé¢æ°ãéã¯åæé¢æ°ã§ãã ãããŠããã¹ãŠãåããã§ãŒã³ã§ãã¬ãŒã¹ã§ããŸã...èµ€ã®é¢æ°ã¯ãSchlechtïŒã¹ã¯ãªããã§åŒã³åºãã®ã¯èŠçã§ãããéåæé¢æ°ã¯JavaScriptã§åŒã³åºãã®ã¯èŠçã§ãã
ãããŠãéãé¢æ°ã®äžã«èµ€ãé¢æ°ãæžãããšã¯ã§ããŸããã ããã«ã€ããŠã¯åŸã§è©³ãã説æããŸãã

ãªãçãã®ã§ããïŒ éåæé¢æ°ãåŒã³åºããŠèšè¿°ãããšããã©ãããçã¿ãçããŸããïŒ
æ¡ä»¶æŒç®åãã«ãŒãããªã¿ãŒã³ã®åäœã¯ç°ãªããŸãã try / catchã¯æ©èœããŸãããéåæé¢æ°ã¯æœè±¡åãå£ããŸãã
åé ç®ã«ã€ããŠããå°ãã

shouldProcessãšprocessãåæé¢æ°ã§ãããæ¡ä»¶ä»ãæŒç®åãæ©èœããåæã³ãŒãã¯æ¬¡ã®ããã«ãªããŸããäžè¬çã«ã¯ããã¹ãŠãåé¡ãããŸããã
åãã§ãããéåæã®å Žåã¯æ¬¡ã®ããã«ãªããŸãã

ååž°ãçŸããç¶æ ããã©ã¡ãŒã¿ãŒãé¢æ°ã«æž¡ããŸãã äžè¬çã«ãèŠãããšã¯æ¬åœã«äžå¿«ã§ãã try / catchã¯æ©èœããŸããããŸããtry / catchã§ã³ãŒãã®éåæãããã¯ãã©ãããããšãäŸå€ããã£ããããªãããšã¯ããã£ãŠããŸãã ã³ãŒã«ããã¯ãæž¡ããã€ãã³ããã³ãã©ãåé 眮ããå¿ èŠããããŸããäžè¬çã«ãtry / catchã¯ãããŸãã...
ãããŠãéåæé¢æ°ã¯æœè±¡åãå£ããŸãã ã©ãããæå³ã§ããïŒ ãã£ãã·ã¥ãæžãããšæ³åããŠãã ããã ã¡ã¢ãªå ã«ãŠãŒã¶ãŒãã£ãã·ã¥ãäœæããŸããã ãããŠããã®ãã£ãã·ã¥ããèªã¿åãé¢æ°ããããŸããããã¯ããã¹ãŠãã¡ã¢ãªå ã«ãããããåœç¶åæçã§ãã ææ¥ãæ°åãæ°çŸãæ°ååã®ãŠãŒã¶ãŒãæ¥ãŸãããã®ãã£ãã·ã¥ãRedisã«é 眮ããå¿ èŠããããŸãã ãã£ãã·ã¥ãRedisã«é 眮ãããšãé¢æ°ã¯éåæã«ãªããŸããããã¯ãRedisã«ããéåæã§ããèªã¿åããªãããã§ãã ãããŠãããã«å¿ããŠãã¹ã¿ãã¯å šäœãéåæã«ãªããããåæé¢æ°ãåŒã³åºããã¹ã¿ãã¯å šäœãæžãæããå¿ èŠããããŸãã äžéšã®æ©èœããã£ãã·ã¥ããã®èªã¿åãæ©èœã«äŸåããŠããå Žåããããéåæã«ãªããŸãã
äžè¬çã«ãJavaScriptã®éåææ§ã«ã€ããŠèšãã°ãããã§ã¯ãã¹ãŠãæ²ãããšèšããŸãã
ããããç§ãã¡ã¯éåææ§ã«ã€ããŠäœãããŠããã®ã§ããããïŒ æåŸã«ãç§ã«ã€ããŠå°ã話ããŸãããã

ã¿ããªãæãããã«æ¥ãŸããã ããŠãç§ã¯ãããããããšããŸãã
ç§ã®ååã¯ã¢ã³ãã¬ã€ã§ãããã«ãªã³ã®çç£çãªã¢ãã€ã«ã®ã¹ã¿ãŒãã¢ããã§åããŠããŸãã ç§ã¯ã¢ã¹ã¯ã¯JSã®çµç¹ãæ¯æŽããRadioJSã®å ±åãã¹ãã§ãã JavaScriptã ãã§ãªããéåææ§ã®ãããã¯ã«ãéåžžã«èå³ããããŸããååãšããŠããããèšèªã®æ±ºå®çãªç¬éã ãšæããŸãã èšèªãéåæã§æ©èœããæ¹æ³ã«ãã£ãŠããã®æåãšã人ã ãèšèªãåãã§å¿«é©ã«äœ¿çšããæ¹æ³ã決ãŸããŸãã
ç¹ã«JavaScriptã®éåææ§ã«ã€ããŠèšãã°ãåžžã«çžäºäœçšãã2ã€ã®ã·ããªãªãããããã«æããŸãã ããã¯ãè€æ°ã®ã€ãã³ããåŠçããåäžã®éåææäœãåŠçããŠããŸãã
å€ãã®ã€ãã³ãïŒããšãã°ãDOMã€ãã³ãããµãŒããŒãžã®æ¥ç¶ïŒã¯ãå€ãã®çš®é¡ã®ã€ãã³ããçºçããããã®ã§ãã
åäžã®æäœã¯ãããšãã°ãããŒã¿ããŒã¹ããã®èªã¿åãã§ãã åäžã®éåææäœã¯ã1ã€ã®çµæãŸãã¯ãšã©ãŒãè¿ããŸãã ãã以äžã®ãªãã·ã§ã³ã¯ãããŸããã
ãããŠããããã®2ã€ã®ã·ããªãªã«ã€ããŠèšãã°ãæšæž¬ããã®ã¯èå³æ·±ãã§ããããã§ã¯ãéåææ§ãæªããäžè¬çã«ã¯ãã¹ãŠãæ²ãã...ã§ããç§ãã¡ã¯æ¬åœã«äœã欲ããã®ã§ããããïŒ å®ç§ãªéåæã³ãŒãã¯ã©ã®ãããªãã®ã§ããããïŒ

ãããŠãç§ãã¡ã¯ãå¶åŸ¡ã®æµããå¶åŸ¡ããããšæã£ãŠããŸãã æ¡ä»¶ä»ãã¹ããŒãã¡ã³ããã«ãŒãã¯ãéåæã³ãŒããšåãããã«åæã³ãŒãã§ãæ©èœããããã«ããŸãã
äŸå€åŠçãå¿ èŠã§ãã éåææäœã§äœ¿çšã§ããªãå Žåããªãtry / catchãå¿ èŠã§ããïŒ ããã¯ãã å¥åŠã§ãã
ãããŠããã¡ãããåäžã®ã€ã³ã¿ãŒãã§ãŒã¹ãæã€ããšãæãŸããã§ãã éåæé¢æ°ãåæé¢æ°ãšã¯ç°ãªãæ¹æ³ã§èšè¿°ããŠåŒã³åºãå¿ èŠãããã®ã¯ãªãã§ããïŒ ãã¹ãã§ã¯ãããŸããã
ãããç§ãã¡ã®æã¿ã§ãã
ä»æ¥ãç§ãã¡ã«ã¯äœããããå°æ¥ã©ã®ãããªããŒã«ãç»å ŽããŸããïŒ

ECMAScript 6ã«ã€ããŠè©±ããŠããå ŽåïŒååãšããŠããããä»æ¥èª¬æããå 容ã§ãïŒãå€ãã®ã€ãã³ããåŠçããããã®EventEmitterãšStreamãContinuation Passing StyleïŒåäžã®éåææäœãåŠçããããã®ã³ãŒã«ããã¯ïŒããããŸãã iïŒãçŽæãšã³ã«ãŒãã³ã

ECMAScript 7ã«ã¯ãå€ãã®ã€ãã³ããåŠçããããã®éåæãžã§ãã¬ãŒã¿ãŒãšãåäžã®éåææäœãåŠçããããã®Async / AwaitããããŸãã
ããã«ã€ããŠã話ããŸãã
ãŸããECMAScript 6ã«ããã®ã¯ãå€ãã®éåæã€ãã³ããåŠçããããšã§ãã ããšãã°ãããŠã¹ã€ãã³ããããŒã¹ãããŒã¯ã®åŠçãæãåºãããŠãã ããã Node.jsã®ãã©ãŠã¶ãŒã«å®è£ ãããŠããEventEmitterãã¿ãŒã³ããããŸãã å€ãã®ã€ãã³ããåŠçããã»ãšãã©ãã¹ãŠã®APIã«ãããŸãã EventEmitterã¯ãã€ãã³ããçºè¡ããåã¿ã€ãã®ã€ãã³ãã«ãã³ãã©ãŒãã¢ã¿ãããããªããžã§ã¯ããäœæã§ããããšã瀺ããŠããŸãã

ã€ã³ã¿ãŒãã§ã€ã¹ã¯éåžžã«ã·ã³ãã«ã§ãã EventListenerãè¿œå ããã€ãã³ãã®ååã§EventListenerãåé€ããããã«ã³ãŒã«ããã¯ãæž¡ããŸãã

ããšãã°ãXMLHttpRequestã§ãå€ãã®ã€ãã³ãã«ã€ããŠè©±ããšããå€ãã®é²è¡ã€ãã³ããæã€ããšãã§ããããšãæå³ããŸãã ã€ãŸã AJAXãªã¯ãšã¹ãã䜿çšããŠäžéšã®ããŒã¿ãããŒããããšãé²è¡ã€ãã³ããçºçããããŒããäžæ¢ããšã©ãŒã€ãã³ãã1åçºçããŸãã

ãšã©ãŒã¯ããŠãŒã¶ãŒã«ãšã©ãŒãéç¥ããããã®ç¹å¥ãªã€ãã³ããEventEmittersããã³Streamsã®ãŠãããŒãµã«ã€ãã³ãã§ãã
å€ãã®å®è£ ããããŸãïŒ

ããã«ãªã¹ããããŠããã®ã¯ã»ãã®äžéšã§ãããã¬ããŒãã®æåŸã«ãããããã¹ãŠã®å®è£ ãããå Žæãžã®ãªã³ã¯ããããŸãã
Node.jsã§ã¯ãEventEmitterã¯ããã©ã«ãã§çµã¿èŸŒãŸããŠãããšèšãããšãéèŠã§ãã
ãã®ãããããã¯Node.jsã®APIãšãã©ãŠã¶ã«ã»ãŒæšæºã§åãã£ãŠãããã®ã§ãã
å€ãã®ã€ãã³ãã«å¯ŸåŠããã«ã¯ä»ã«äœãå¿ èŠã§ããïŒ ã¹ããªãŒã
ã¹ããªãŒã ã¯ããŒã¿ã¹ããªãŒã ã§ãã ããŒã¿ãšã¯ïŒ ããšãã°ããã¡ã€ã«ã®ããŒã¿ãããã¹ãããŒã¿ããŸãã¯ãªããžã§ã¯ããã€ãã³ããªã©ã®ãã€ããªããŒã¿ã§ãã æãäžè¬çãªäŸïŒ

ã¹ããªãŒã ã«ã¯ããã€ãã®ã¿ã€ãããããŸãã

ããã§ã¯ãã¹ã¿ã€ã©ã¹ãã¡ã€ã«ããcssãã¡ã€ã«ãžã®å€æã®ãã§ãŒã³ãèŠãŠãèªåãã¬ãã£ãã¯ã¹ãè¿œå ããŸããããã¯ãAndrei SitnikãšåœŒã®èªåãã¬ãã£ãã¯ã¹ã倧奜ãã ããã§ãã
ããã€ãã®ã¿ã€ãã®ã¹ããªãŒã ãããããšãããããŸãããœãŒã¹ã¹ããªãŒã ã¯gulp.srcã§ããããã¡ã€ã«ãèªã¿åãããã¡ã€ã«ãªããžã§ã¯ããçæããå€æã¹ããªãŒã ã«é²ã¿ãŸãã æåã®å€æã¹ããªãŒã ã¯ã¹ã¿ã€ã©ã¹ããcssãã¡ã€ã«ãäœæãã2çªç®ã®å€æã¹ããªãŒã ã¯ãã¬ãã£ãã¯ã¹ãè¿œå ããŸãã ãããŠæåŸã®ã¿ã€ãã®ã¹ããªãŒã ã¯ã³ã³ã·ã¥ãŒããŒã¹ããªãŒã ã§ããããã®ãªããžã§ã¯ããåãåãããã£ã¹ã¯ã®ã©ããã«äœããæžã蟌ã¿ãäœãåºåããŸããã

ã€ãŸã ããŒã¿ãœãŒã¹ãå€æãã³ã³ã·ã¥ãŒãã®3çš®é¡ã®ãããŒããããŸãã ãããŠããããã®ãã¿ãŒã³ã¯ãgulpã ãã§ãªããDOMã€ãã³ãã芳å¯ãããšãã«ãã©ãã§ããã¬ãŒã¹ã§ããŸãã ããããå€æããDOMã€ãã³ããçºè¡ããã¹ã¬ãããããããããã®DOMã€ãã³ããæ¶è²»ãããšç¹å®ã®çµæãè¿ãããŸãã
ããã¯ãã³ã³ãã¢ãšåŒã°ãããã®ã§ãã ã¹ã¬ããã®å©ããåããŠããªããžã§ã¯ãããã€ãã©ã€ã³ã®æåã®ã©ããã«é 眮ãããå€æã®ãã§ãŒã³ãééãã人ã ãããã«å°éãããšãäœããå€æŽããè¿œå ããåé€ããæçµçã«äœããåŸããšãã«ããã®ãããªãã§ãŒã³ãæ§ç¯ã§ããŸãè»ã
ããã€ãã®ã¹ããªãŒã å®è£ ããããŸãããŸãã¯ããããã¯Observableã§ãã

Node.jsã§ã¯ãã¹ããªãŒã ã¯çµã¿èŸŒã¿ã§ã-ãããã¯ããŒãã¹ããªãŒã ã§ãã
ãããã£ãŠãEventEmitterãšStreamããããŸãã EventEmitterã¯ããã¹ãŠã®APIã§ãããã©ã«ãã§èšå®ãããŠããŸãã ã¹ããªãŒã ã¯ãè€æ°ã®ã€ãã³ããåŠçããããã®ã€ã³ã¿ãŒãã§ã€ã¹ãçµ±åããããã«äœ¿çšã§ããã¢ããªã³ã§ãã

éåæAPIãæ¯èŒããåºæºã«ã€ããŠèª¬æãããšãæŠããŠãreturnã¹ããŒãã¡ã³ããloopã¹ããŒãã¡ã³ãã¯æ©èœããŸãããtry/ catchã¯æ©èœããŸããããã¡ãããåææäœãè¡ãåäžã®ã€ã³ã¿ãŒãã§ãŒã¹ãšã¯ã»ã©é ãã§ãã ã
äžè¬ã«ãECMAScript 6ã§å€ãã®ã€ãã³ããåŠçããã«ã¯ããã¹ãŠãããŸãè¯ããããŸããã
åäžã®éåææäœã«ã€ããŠè©±ããšããECMAScript 6ã«ã¯3ã€ã®ã¢ãããŒãããããŸãã

ç¶ç¶åæž¡ãã¹ã¿ã€ã«ã¯ãã³ãŒã«ããã¯ã§ããããŸãã

ã¿ãªããã¯ããã«æ £ããŠãããšæããŸãã ããã¯ãéåæãªã¯ãšã¹ããäœæããããã«ã³ãŒã«ããã¯ãæž¡ããšãã§ããã³ãŒã«ããã¯ã¯ããšã©ãŒãŸãã¯çµæãšãšãã«åŒã³åºãããŸãã ããã¯äžè¬çãªã¢ãããŒãã§ãããNodeã®ãã©ãŠã¶ã«ããããŸãã

ãã®ã¢ãããŒãã®åé¡ã¯ãããªãããã¹ãŠãç解ããŠãããšæããŸãã

ããã¯ããã¹ãŠã®æ©èœãåæã§ããå ŽåããŠãŒã¶ãŒã®ãã€ãŒããã£ãŒããéåæã§åä¿¡ããæ¹æ³ã§ãã

åãã³ãŒãã§ãããåæçã«ã¯æ¬¡ã®ããã«ãªããŸãã

ãã©ã³ããå¢ãããŠãèŠãããããããšãã§ããŸãã ãããŠãããå°ãå¢ãããŠãã ãã...ãããŠããããSchlechtïŒScriptã§ããããšãããèªèããŠããŸãã

圌ãã¯ç§ã®èããçãã ãšèšã£ãã
Continuation Passing Styleã¯Node.jsã®æšæºãã©ãŠã¶APIã§ãããç§ãã¡ã¯ããããåžžã«äœ¿çšããŠããŸãããäžäŸ¿ã§ãã ãããã£ãŠãçŽæããããŸãã ããã¯ãéåææäœã§ãããªããžã§ã¯ãã§ãã

Promiseã¯ãå°æ¥ãéåæçã«äœãããããšããçŽæã®ãããªãã®ã§ãã

thenã¡ãœããã䜿çšããŠã³ãŒã«ããã¯ãéåææäœã«ã¢ã¿ããã§ããŸããããã¯ãååãšããŠãAPIã®ã¡ã€ã³ã¡ãœããã§ãã ãããŠãéåžžã«éèŠãªããšã«ãPromiseããã§ãŒã³ã§ããŸãããã®åŸãé çªã«åŒã³åºãããšãã§ãããã®åŸã«æž¡ããããã¹ãŠã®é¢æ°ãPromiseãè¿ãããšãã§ããŸãã ããã¯ãPromisesã®Twitterããã®ãŠãŒã¶ãŒãã£ãŒããªã¯ãšã¹ãã®å€èŠ³ã§ãã
ãã¡ããããã®ã¢ãããŒããContinuation Passing Styleãšæ¯èŒãããšãPromisesã®æ¹ã䜿ããããããšã¯èšããŸã§ããããŸãããå®åæãæžãæ©äŒãå°ãªããªããŸãã
Continuation Passing Styleã¯ãNode.jsãio.jsã®ãã¹ãŠã®APIã§åŒãç¶ãããã©ã«ãã§äœ¿çšãããŠãããããã€ãã®çç±ã§Promisesã«åãæ¿ããããšããèšç»ããŠããŸããã æåã¯ãå€ãã®äººãçç±ã¯ããã©ãŒãã³ã¹ã ãšèšã£ãŠããŸããã å®éã2013幎ã®èª¿æ»ã§ã¯ãçŽæã¯ã³ãŒã«ããã¯ãããã¯ããã«é ããŠããããšã瀺ãããŠããŸãã ããããbluebirdãªã©ã®ã©ã€ãã©ãªã®åºçŸã«ãããbluebirdã®Promiseã®ããã©ãŒãã³ã¹ã¯ã³ãŒã«ããã¯ã«è¿ããããããã§ã¯ãªããšèªä¿¡ãæã£ãŠèšããŸãã éèŠãªãã€ã³ãïŒãªãPromisesããããŸã§APIã§ã®äœ¿çšãæšå¥šãããªãã®ã§ããïŒ APIããPromiseãçºè¡ãããšãå®è£ ã匷å¶ãããããã§ãã
ãã¹ãŠã®Promisesã©ã€ãã©ãªã¯æšæºã«åŸãå¿ èŠããããŸãããPromisesãçºè¡ãããšãã¯ãå®è£ ãçºè¡ããŸãã é ãPromiseã䜿çšããŠã³ãŒããèšè¿°ããAPIããé ãPromiseãçºè¡ããå ŽåããŠãŒã¶ãŒã«ãšã£ãŠããŸãå¿«é©ã§ã¯ãããŸããã ãããã£ãŠãå€éšAPIã«ã€ããŠã¯ããã¡ãããã³ãŒã«ããã¯ã®äœ¿çšãæšå¥šããŠããŸãã

ããããã®Promiseså®è£ ããããå®è£ ãæžããŠããªããªããããªãã¯æ¬åœã®JavaScriptããã°ã©ããŒã§ã¯ãããŸããã Promiseã®å®è£ ãäœæããªãã£ããããèªåã®èšèªãèãåºãå¿ èŠããããŸããã
ãã®ãããäžè¬ã«ãPromisesã¯å®åæãããããã«å°ããã§ãããããã§ããŸã ããã»ã©è¯ããããŸããã
ã³ã«ãŒãã³ã¯ã©ãã§ããïŒ èå³æ·±ããã®ã¯ãã§ã«ããããå§ãŸã£ãŠããŸãã æ³åããŠã¿ãŠ...
ããã¯é¢çœãã§ãã ç§ãã¡ã¯ãããã¹ãã®JSConfã«ããŸããããJavaScriptãªã©ã§ã¯ã¢ããã³ãã¿ãŒãããã°ã©ã ããã¯ã¬ã€ãžãŒãªäººãããŠã圌ãèŠããããšããããšã®ååã¯æåããŸããã§ããã ãããã£ãŠã圌ã¯çµ¶ããèšããŸããïŒãOKãä»æ³åããŠãã ãã...ãã®ã¯ã¢ããã³ãã¿ãŒãé¢éžãããã¹ãŠãããŸããã£ã...ãã
å®è¡ã®ããæç¹ã§é¢æ°ãäžæåæ¢ã§ãããšæ³åããŠãã ããã

ããã§ãé¢æ°ã¯ãŠãŒã¶ãŒåãåãåããããŒã¿ããŒã¹ã«ã¯ããŒã«ãããŠãŒã¶ãŒãªããžã§ã¯ããåãåããååãè¿ããŸãã åœç¶ããããŒã¿ããŒã¹ã«ã¢ã¯ã»ã¹ã-getUseré¢æ°ã¯éåæã§ãã getUserãåŒã³åºããããšãã«getUserNameé¢æ°ãäžæåæ¢ã§ãããã©ãã§ããããïŒ ããã§ãgetUserNameé¢æ°ãå®è¡ããgetUserã«ã¢ã¯ã»ã¹ããŠåæ¢ããŸããã getUserã¯ããŒã¿ããŒã¹ã«ã¢ã¯ã»ã¹ãããªããžã§ã¯ããåãåãããããé¢æ°ã«è¿ããå®è¡ãç¶ããŸãã ããã¯ã©ãã»ã©ã¯ãŒã«ã§ãããã
å®éãã³ã«ãŒãã³ã¯ç§ãã¡ã«ãã®æ©äŒãäžããŠãããŸãã ã³ã«ãŒãã³ã¯ããã€ã§ãäžæåæ¢ããã³åéã§ããæ©èœã§ãã éèŠãªãã€ã³ãïŒããã°ã©ã å šäœãåæ¢ããŸããã

ããã¯ããããã³ã°æäœã§ã¯ãããŸããã ç¹å®ã®å Žæã§ç¹å®ã®æ©èœã®å®è¡ãåæ¢ããŸãã

javascriptãžã§ãã¬ãŒã¿ãŒã䜿çšãããšãgetUserNameã¯ã©ã®ããã«ãªããŸããïŒ é¢æ°ããžã§ãã¬ãŒã¿ãŒãè¿ãããšã瀺ãããã«ãé¢æ°å®£èšã«ã*ããè¿œå ããå¿ èŠããããŸãã é¢æ°ãäžæåæ¢ããå Žæã§yieldããŒã¯ãŒãã䜿çšã§ããŸãã ãããŠãããã§getUserãPromiseãè¿ãããšãèŠããŠããããšãéèŠã§ãã
ãªããªã ãžã§ãã¬ãŒã¿ãŒã¯ããšããšJavaScriptã§é 延ã·ãŒã±ã³ã¹ãå®è¡ããããã«çºæããããã®ã§ãæŠããŠãããããåæã³ãŒãã«äœ¿çšããã®ã¯ããã¯ã§ãã ãããã£ãŠãäœããã®åœ¢ã§ãããè£ãã©ã€ãã©ãªãå¿ èŠã§ãã

ããã§ã¯ããcoãã䜿çšããŠãžã§ãã¬ãŒã¿ãŒãã©ããããéåæé¢æ°ãè¿ããŸãã
åèšã以äžãåŸãããŸãïŒ

ifãforããã®ä»ã®æŒç®åã䜿çšã§ããé¢æ°ããããŸãã
å€ãè¿ãã«ã¯ãåæé¢æ°ã®å Žåãšåæ§ã«ãåã«returnãèšè¿°ããŸãã å éšã§try / catchã䜿çšã§ããäŸå€ããã£ããããŸãã

getUserã䜿çšããPromisesããšã©ãŒã§è§£æ±ºããå Žåãããã¯äŸå€ãšããŠã¹ããŒãããŸãã
getUserNameé¢æ°ã¯Promiseãè¿ããããPromiseãšåãããã«æäœã§ããŸããthenãchainãªã©ã䜿çšããŠã³ãŒã«ããã¯ãåæã§ããŸãã
ããããç§ãèšã£ãããã«ãéåæã³ãŒãã«ãžã§ãã¬ãŒã¿ãŒã䜿çšããããšã¯ããã¯ã§ãã ãããã£ãŠãå€éšAPIãšããŠå ¬éããããšã¯æãŸãããããŸããã ãã ããã¢ããªã±ãŒã·ã§ã³å ã§ã®äœ¿çšã¯åé¡ãªãã®ã§ãã³ãŒãã転眮ã§ããå Žåã¯äœ¿çšããŠãã ããã

å€ãã®å®è£ ããããŸãã ãã§ã«æšæºã®äžéšã§ãããžã§ãã¬ãŒã¿ãŒã䜿çšããŠãããã®ããããŸãããNode.jsã§åäœãããžã§ãã¬ãŒã¿ãŒã䜿çšããªããã¡ã€ããŒããããç¬èªã®åé¡ããããŸãã
äžè¬ã«ãããã¯åäžã®éåææäœãæäœããããã®3çªç®ã®ã¢ãããŒãã§ãããããã¯äŸç¶ãšããŠããã¯ã§ãããåæã«è¿ãã³ãŒããæ¢ã«äœ¿çšã§ããŸãã æ¡ä»¶ã¹ããŒãã¡ã³ããã«ãŒããããã³try / catchãããã¯ã䜿çšã§ããŸãã

ã€ãŸã åäžã®éåææäœã§åäœããECMAScript 6ã¯ãç®çã®çµæã«å°ãè¿ã¥ããŠããããã«èŠããŸãããç¹å¥ãªã*ããèšè¿°ããŠããŒæŒç®åãyieldãã䜿çšããå¿ èŠããããããã³ã«ãŒãã³ã§ããåäžã®ã€ã³ã¿ãŒãã§ã€ã¹ã®åé¡ã¯è§£æ±ºãããŸããã

ãã®ãããECMAScript 6ã§ã¯ãå€ãã®ã€ãã³ããåŠçããããã«ãåäžã®éåææäœïŒCPSãPromiseãCoroutinesïŒãåŠçââããEventEmitterãšStreamããããŸãã ãããŠãããã¯ãã¹ãŠçŽ æŽãããããã§ãããäœããæ¬ ããŠããŸãã ãã£ãšåæ°ãããã倧èã§ãæ°ãããã®ã欲ãããé©åœã欲ããã
ãããŠãES7ãæžãã人ãã¡ã¯ç§ãã¡ã«é©åœãèµ·ããããšã«æ±ºããAsync / AwaitãšAsync GeneratorsããããããŸããã

Async / Awaitã¯ãããšãã°ããŒã¿ããŒã¹ã¯ãšãªãªã©ã®åäžã®éåææäœã§äœæ¥ã§ããããã«ããæšæºã§ãã
ãããããžã§ãã¬ãŒã¿ãŒã§getUserNameãäœæããæ¹æ³ã§ãã

ãããŠãããã¯åãã³ãŒããAsync / Awaitã§ã©ã®ããã«èŠãããã§ãïŒ

ãã¹ãŠãéåžžã«ãã䌌ãŠãããæŠããŠãããã¯ããã¯ããæšæºãžã®äžæ©ã§ãã ããã«ããŒã¯ãŒããasyncãããããŸããããã¯ãé¢æ°ãéåæã§ãããPromiseãè¿ãããšã瀺ããŠããŸãã éåæé¢æ°å ã§ã¯ãPromiseãè¿ãawaitããŒã¯ãŒãã䜿çšã§ããŸãã ãããŠããã®Promiseãå®äºããã®ãåŸ ã€ããšãã§ããŸããé¢æ°ãäžæåæ¢ããŠããã®Promiseãå®äºããã®ãåŸ ã€ããšãã§ããŸãã

ãŸããæ¡ä»¶æãã«ãŒããtry / catchã®æ©èœããããŸããã€ãŸããéåæé¢æ°ã¯ES7ã§åæ³åãããŸãã ããã§ãé¢æ°ãéåæã®å ŽåãããŒã¯ãŒããasyncããè¿œå ãããšæ瀺çã«èšããŸãã ãããŠãããã¯ååãšããŠããã»ã©æªãã¯ãããŸããããããã§ãåäžã®ã€ã³ã¿ãŒãã§ãŒã¹ã¯ãããŸããã
ããããã®ã€ãã³ãã¯ã©ãã§ããïŒ ããã«ã¯ãéåæãžã§ãã¬ãŒã¿ãŒãšåŒã°ããæšæºããããŸãã
äžè¬çã«ãå€æ°ãšã¯äœã§ããïŒ JavaScriptã§å€æ°ãã©ã®ããã«äœ¿çšããŸããïŒ

å€ãã®ããšãã«ãŒãã§åŠçããã®ã§ãã«ãŒãã§å€ãã®ã€ãã³ããåŠçããŸãããã

éåæé¢æ°å ã§ã¯ã... onæ§æã®ããŒã䜿çšã§ããŸããããã«ãããéåæã³ã¬ã¯ã·ã§ã³ãå埩åŠçã§ããŸãã ã®ãããªã
ãã®äŸã§ã¯ãobserveã¯å埩å¯èœãªãã®ãã€ãŸã ãŠãŒã¶ãŒãããŠã¹ãåãããã³ã«ãmousemoveã€ãã³ããçºçããŸãã ãã®ãµã€ã¯ã«ã«é¥ããäœããã®åœ¢ã§ãã®ã€ãã³ããåŠçããŸãã ãã®å Žåãç»é¢ã«ããã·ã¥ãæç»ããŸãã

ãªããªã é¢æ°ã¯éåæã§ããPromiseãè¿ãããšãç解ããããšãéèŠã§ãã ããããããšãã°ãäœããã®æ¹æ³ã§Webãœã±ããããã®ã¡ãã»ãŒãžãåŠçãããããããããã£ã«ã¿ãªã³ã°ãããããå Žåã«ãå€ãã®å€ãè¿ãããå Žåã¯ã©ãã§ããããã ã€ãŸã ãããããããåºå£ã«ã¯ãããããããŸãã ããã§ã¯éåæãžã§ãã¬ãŒã¿ã圹ç«ã¡ãŸãã ãéåæé¢æ°*ããšèšè¿°ããé¢æ°ãéåæã§ãããšèšããäœããã®ã»ãããè¿ããŸãã

ãã®å ŽåãWebãœã±ããã®Messageã€ãã³ãã調ã¹ãŠãçºçãããã³ã«äœããã®æ€èšŒãè¡ããæ€èšŒã«åæ Œãããšãè¿ãããã³ã¬ã¯ã·ã§ã³ã«æ»ããŸãã ãã®ã¡ãã»ãŒãžãè¿œå ããæ¹æ³ã

ããã«ãããã¯ãã¹ãŠéåæã«è¡ãããŸãã ã¡ãã»ãŒãžã¯èç©ããããå°çãããšè¿ãããŸãã ãããŠãããã§ãã¹ãŠã®æ¡ä»¶ã¹ããŒãã¡ã³ããã«ãŒããããã³try / catchãæ©èœããŸãã
質åïŒfilterWSMessagesã¯äœãè¿ããŸããïŒ

ããã¯ééããªãPromiseã§ã¯ãããŸãããããã¯ããçš®ã®ã³ã¬ã¯ã·ã§ã³ã ããã§ãããã®ãããªãã®âŠãããããã¯é åã§ããããŸããã

ããã«ã ã€ãã³ããçæãããããã®èŠ³å¯ã¯äœãè¿ããŸããïŒ
ãããŠã圌ãã¯ãããã Observablesãªããžã§ã¯ãã ããã¯æ°ããèšèã§ãããæŠããŠãObservableã¯ã¹ããªãŒã ã§ããããããã¯ã¹ããªãŒã ã§ãã ãããã£ãŠãåã¯éããŸãã
åèšã§ãéåæã®åäžæäœãæäœããããã®Async / Awaitãå€ãã®æäœãè¡ãããã®Async GeneratorããããŸãã
ç§ãã¡ãæ®ãããã®ãšã©ãããæ¥ãã®ããå°ãæ¯ãè¿ã£ãŠã¿ãŸãããã
ãã€ãŒããã£ãŒããååŸããã«ã¯ãCPSã§æ¬¡ã®ã³ãŒããèšè¿°ããŸãã

å€ãã®å®åæããšã©ãŒåŠçãå®éã®ããã¥ã¢ã«ããããŠäžè¬çã«ã¯ããŸãå¿«é©ã§ã¯ãããŸããã
Promiseã§ã¯ãã³ãŒãã¯æ¬¡ã®ããã«ãªããŸãã

ããå°ããªãã€ã©ãŒãã¬ãŒãã§ã1ãæã§äŸå€ãåŠçã§ããŸããããã¯æ¢ã«é©åã§ãããããã§ããããã¯ååšããŸã... try / catchãæ¡ä»¶æŒç®åãæ©èœããŸããã
Async / Awaitã䜿çšãããšã次ã®æ§é ãåŸãããŸãã

ãããŠãã»ãŒåãããã«ã圌ãã¯ç§ãã¡ã«ã³ã«ãŒãã³ãäžããŸãã
ããã§ã¯ããã®é¢æ°ããéåæããšããŠå®£èšããå¿ èŠãããããšãé€ããŠããã¹ãŠãçŽ æŽãããã§ãã
ã€ãã³ãã®ã»ããã«é¢ããŠãDOMã€ãã³ãã«ã€ããŠè©±ããŠããå Žåãããã¯mousemoveãåŠçããEventEmitterã䜿çšããŠç»é¢ã«æç»ããæ¹æ³ã§ãã

, Stream' Kefir :

mousemove window, - , callback . , end stream', DOM,
Async Generators :

, - .
-, .
, , , .
- , .. , Streams , , Async Generators, .
- , , AJAX-, , , Promises.
- . , Async/Await Async Generators. API, , Promise API callback'.
- , error EventEmitter'.
- Promise.all ..
, Schlecht!Script, GitHub .., , - , â , , , , , - , , , , Promises.
é£çµ¡å
» twitter
» filipovskii_off
â - FrontendConf . 2017 .
Frontend Conf , , . , 8 , .
HighLoad++ 11 « », :
- NASA JavaScript / (Liberty Global);
- 16 / (Beta Digital Production);
- . - / (SuperJob);