{"id":4251,"date":"2017-06-30T13:58:25","date_gmt":"2017-06-30T04:58:25","guid":{"rendered":"https:\/\/kumapress.com\/wp\/?p=4251"},"modified":"2017-07-17T14:54:18","modified_gmt":"2017-07-17T05:54:18","slug":"node%e3%81%aepromise%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e8%80%83%e5%af%9f","status":"publish","type":"post","link":"https:\/\/kumapress.com\/?p=4251","title":{"rendered":"node\u306ePromise\u306e\u4f7f\u3044\u65b9\u8003\u5bdf"},"content":{"rendered":"<p><span style=\"font-size: 14pt;\">node.js\u3092\u4f7f\u3063\u3066\u3044\u308b\u3068\u3001\u7279\u306b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306a\u3093\u304b\u3092\u4f7f\u3063\u3066\u3044\u308b\u3068\u3001callback\u3067\u306e\u30cd\u30b9\u30c8\u306e\u6df1\u3055\u306b\u8f9f\u6613\u3057\u3066\u304d\u307e\u3059\u3002<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">\u305d\u3093\u306a\u3068\u304d\u306b\u6d3b\u8e8d\u3057\u3066\u304f\u308c\u308b\u306e\u304cPromise\u3002<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">\u691c\u7d22\u3057\u3066\u307f\u308b\u3068\u3001\u8a18\u4e8b\u306f\u4e00\u676f\u51fa\u3066\u308b\u3093\u3067\u3059\u304c\u3001\u3082\u4e00\u3064\u4f7f\u3044\u65b9\u304c\uff1f\uff1f\u3060\u3063\u305f\u3093\u3067\u3001\u3042\u308c\u3053\u308c\u3068\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/span><\/p>\n<h2>\u57fa\u672c\u306a\u30bd\u30fc\u30b9\u306e\u3054\u7d39\u4ecb<\/h2>\n<p><span style=\"font-size: 14pt;\">\u3053\u3093\u306a\u30b7\u30fc\u30f3\u3092\u60f3\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">\uff11\uff09\u30b7\u30b9\u30c6\u30e0\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080<\/span><br \/>\n<span style=\"font-size: 14pt;\">\uff12\uff09\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u958b\u59cb\u3059\u308b<\/span><br \/>\n<span style=\"font-size: 14pt;\">\uff13\uff09\u66f4\u65b0\u3059\u308b<\/span><br \/>\n<span style=\"font-size: 14pt;\">\uff14\uff09\u30a8\u30e9\u30fc\u6642\u306e\u51e6\u7406\u3092\u3059\u308b<\/span><br \/>\n<span style=\"font-size: 14pt;\">\uff15\uff09\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u958b\u653e\u3059\u308b<\/span><br \/>\n<span style=\"font-size: 14pt;\">\uff16\uff09\u5f8c\u59cb\u672b\u3092\u3059\u308b<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">\u30b3\u30ec\u3092\u60f3\u5b9a\u3057\u305f\u30bd\u30fc\u30b9(Express\u4f7f\u3063\u3066\u3044\u307e\u3059)<br \/>\n\u203b\u4ee5\u4e0b\u3001\u57fa\u672c\u30bd\u30fc\u30b9\u3068\u3057\u307e\u3059\u3002<\/span><\/p>\n<pre><span style=\"font-size: 14pt;\">router.get('\/', function(req, res, next) {<\/span>\r\n<span style=\"font-size: 14pt;\"> var trn = new Promise( function( resolve, reject ){<\/span>\r\n<span style=\"font-size: 14pt;\">   console.log( \"step1\" );<\/span>\r\n<span style=\"font-size: 14pt;\">   resolve();<\/span>\r\n<span style=\"font-size: 14pt;\"> }).then( function() {<\/span>\r\n<span style=\"font-size: 14pt;\">   return new Promise( function( resolve, reject ){<\/span>\r\n<span style=\"font-size: 14pt;\">     console.log( \"step2\" );<\/span>\r\n<span style=\"font-size: 14pt;\">     resolve();<\/span>\r\n<span style=\"font-size: 14pt;\">   });<\/span>\r\n<span style=\"font-size: 14pt;\"> }).then( function() {<\/span>\r\n<span style=\"font-size: 14pt;\">   return new Promise( function( resolve, reject ){<\/span>\r\n<span style=\"font-size: 14pt;\">     console.log( \"step3\" );<\/span>\r\n<span style=\"font-size: 14pt;\">     resolve();<\/span>\r\n<span style=\"font-size: 14pt;\">   });<\/span>\r\n<span style=\"font-size: 14pt;\"> }).then( function() {<\/span>\r\n<span style=\"font-size: 14pt;\">   console.log( \"step4\" );<\/span>\r\n<span style=\"font-size: 14pt;\"> }).catch( function( error ) {<\/span>\r\n<span style=\"font-size: 14pt;\">   console.log( \"catch1:\"+error );<\/span>\r\n<span style=\"font-size: 14pt;\"> }).then( function() {<\/span>\r\n<span style=\"font-size: 14pt;\">   console.log( \"step5\" );<\/span>\r\n<span style=\"font-size: 14pt;\"> }).then( function() {<\/span>\r\n<span style=\"font-size: 14pt;\">   console.log( \"step6\" );<\/span>\r\n<span style=\"font-size: 14pt;\"> });<\/span>\r\n<span style=\"font-size: 14pt;\">});<\/span><\/pre>\n<h2><span style=\"font-size: 14pt;\">\u3055\u3066\u3001\u3084\u3063\u3066\u307f\u307e\u3057\u3087\u3046\uff01<\/span><\/h2>\n<h3><span style=\"font-size: 14pt;\">\u57fa\u672c\u5f62\u3092\u5b9f\u884c\u3059\u308b\u3068\u3002\u3002\u3002<\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">$ node bin\/www<\/span><br \/>\n<span style=\"font-size: 14pt;\">step1<\/span><br \/>\n<span style=\"font-size: 14pt;\">step2<\/span><br \/>\n<span style=\"font-size: 14pt;\">step3<\/span><br \/>\n<span style=\"font-size: 14pt;\">step4<\/span><br \/>\n<span style=\"font-size: 14pt;\">step5<\/span><br \/>\n<span style=\"font-size: 14pt;\">step6<\/span><\/p>\n<p>\u554f\u984c\u306a\u304f\u5168\u90e8\u901a\u3063\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h3><span style=\"font-size: 14pt;\">Step2\u3067reject\u3057\u3066\u307f\u308b\u3068<\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">step1<\/span><br \/>\n<span style=\"font-size: 14pt;\">step2<\/span><br \/>\n<span style=\"font-size: 14pt;\">catch1:step2 error<\/span><br \/>\n<span style=\"font-size: 14pt;\">step5<\/span><br \/>\n<span style=\"font-size: 14pt;\">step6<\/span><\/p>\n<p>\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u98db\u3093\u3067\u304f\u308c\u307e\u3057\u305f\uff01<\/p>\n<h3><span style=\"font-size: 14pt;\">Step2\u3068Step3\u3067Resolve\u3092\u53d6\u3063\u3066\u307f\u308b\u3068<\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">step1<\/span><br \/>\n<span style=\"font-size: 14pt;\">step2<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">\u304a\u304a\u304a\u3001step3\u4ee5\u964d\u3078\u306f\u9032\u307e\u306a\u3044\u3093\u3060\u3002\u3002\u3002<\/span><\/p>\n<h3><span style=\"font-size: 14pt;\">Step4\u306bReject\u3092\u5165\u308c\u3066\u307f\u308b\u3068<\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">step1<\/span><br \/>\n<span style=\"font-size: 14pt;\">step2<\/span><br \/>\n<span style=\"font-size: 14pt;\">step3<\/span><br \/>\n<span style=\"font-size: 14pt;\">step4<\/span><br \/>\n<span style=\"font-size: 14pt;\">catch1:ReferenceError: reject is not defined<\/span><br \/>\n<span style=\"font-size: 14pt;\">step5<\/span><br \/>\n<span style=\"font-size: 14pt;\">step6<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">\u304a\u3063\u3068\u301c\u3001\u30a4\u30d9\u30f3\u30c8\u306f\u98db\u3076\u3051\u3069\u3001\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u3044\u304b\u306a\u3044\u3002\u3002<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">\u3075\u3080\u301c\u3001Promise\u3067\u306e\u9010\u6b21\u51e6\u7406\u3063\u3066\u4fbf\u5229\u306a\u3093\u3067\u3059\u306d\u3047\u3002\u3002\u3002<\/span><\/p>\n<aside class=\"row veu_insertAds after\"><div class=\"col-md-6\"><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- \u7b2c 1 \u9762 - 2\uff08www.kumapress.com\uff09 -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-5987681888830890\"\r\n     data-ad-slot=\"5379270693\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div><div class=\"col-md-6\"><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- \u7b2c 1 \u9762 - 1\uff08www.kumapress.com\uff09 -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-5987681888830890\"\r\n     data-ad-slot=\"7353370294\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div><\/aside>","protected":false},"excerpt":{"rendered":"<p>Node\u3092\u4f7f\u3046\u6642\u3001\u975e\u540c\u671f\u3067\u306a\u304f\u3001\u9010\u6b21\u51e6\u7406\u3057\u3066\u6b32\u3057\u3044\u3068\u304d\u3063\u3066\u6709\u308a\u307e\u3059\u3002<br \/>\n\u305f\u3060\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3084\u901a\u4fe1\u5468\u308a\u306f\u975e\u540c\u671f\u306e\u584a\u306a\u3093\u3067\u3001\u305d\u306e\u307e\u307e\u51e6\u7406\u3059\u308b\u3068\u51c4\u307e\u3058\u3044\u30cd\u30b9\u30c8\u306e\u5d50\u304c(^^;<br \/>\n\u305d\u3093\u306a\u6642\u6d3b\u8e8d\u3057\u3066\u304f\u308c\u308bPromise\u306e\u52d5\u304d\u306b\u3064\u3044\u3066\u5b9f\u9a13\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"vkexunit_cta_each_option":"","footnotes":""},"categories":[143],"tags":[145],"class_list":["post-4251","post","type-post","status-publish","format-standard","hentry","category-node-js","tag-promise"],"_links":{"self":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts\/4251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4251"}],"version-history":[{"count":2,"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts\/4251\/revisions"}],"predecessor-version":[{"id":4256,"href":"https:\/\/kumapress.com\/index.php?rest_route=\/wp\/v2\/posts\/4251\/revisions\/4256"}],"wp:attachment":[{"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kumapress.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}