{"id":10312,"date":"2020-05-03T10:01:46","date_gmt":"2020-05-03T01:01:46","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=10312"},"modified":"2023-05-27T20:56:48","modified_gmt":"2023-05-27T11:56:48","slug":"%e3%80%90javascript%e3%80%91%e3%80%8casync%e3%80%8d%e3%80%81%e3%80%8cawait%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=10312","title":{"rendered":"\u3010JavaScript\u3011\u975e\u540c\u671f\u95a2\u6570\uff08\u300casync\u300d\u3001\u300cawait\u300d\uff09\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>async\u3001await\u3068\u306f\uff1f<\/h2>\n<p>\u975e\u540c\u671f\u51e6\u7406\u3092\u8a18\u8ff0\u3067\u304d\u308b\u6587\u6cd5\u3067\u3001Promise\u3092\u3088\u308a\u7c21\u6f54\u306b\u8a18\u8ff0\u3059\u308b\u4e8b\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002Promise\u306b\u95a2\u3057\u3066\u306f\u4e0b\u8a18\u306e\u8a18\u4e8b\u3067\u89e3\u8aac\u3057\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"IBAUOJBroM\"><p><a href=\"http:\/\/www.code-magagine.com\/?p=5522\">\u3010JavaScript\u3011\u975e\u540c\u671f\u51e6\u7406\u3092\u7c21\u5358\u306b\u3057\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u5730\u7344\u304b\u3089\u8131\u51fa\u3055\u305b\u308b\u300cPromise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u300d\u306b\u3064\u3044\u3066<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/www.code-magagine.com\/?p=5522&#038;embed=true#?secret=IBAUOJBroM\" data-secret=\"IBAUOJBroM\" width=\"500\" height=\"282\" title=\"&#8220;\u3010JavaScript\u3011\u975e\u540c\u671f\u51e6\u7406\u3092\u7c21\u5358\u306b\u3057\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u5730\u7344\u304b\u3089\u8131\u51fa\u3055\u305b\u308b\u300cPromise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u300d\u306b\u3064\u3044\u3066&#8221; &#8212; \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30de\u30ac\u30b8\u30f3\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<h2>async\u3001await\u306e\u4f7f\u3044\u65b9<\/h2>\n<p>resolve\u30e1\u30bd\u30c3\u30c9\u3067\u5024\u3092\u8fd4\u3059Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092<span style=\"color: #ff0000;\"><strong>then\u3092\u4f7f\u308f\u305a\u306b\u305d\u306e\u307e\u307e\u30c7\u30d0\u30c3\u30b0\u3057\u3088\u3046\u3068\u3057\u305f\u3089\u4e0b\u8a18\u306e\u3088\u3046\u306b\u7a7a\u306ePromise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5165\u3063\u3066\u3044\u308b\u3060\u3051\u306b\u306a\u308a\u307e\u3059\u3002\u3082\u3061\u308d\u3093\u3001then\u3092\u4f7f\u3048\u3070resolve\u306e\u5024\u3092\u53d6\u308a\u51fa\u3059\u4e8b\u304c\u53ef\u80fd\u3067\u3059\u304c\u3001async\u3001await\u3092\u4f7f\u3063\u3066\u3082\u53d6\u308a\u51fa\u3059\u4e8b\u304c\u53ef\u80fd\u3067\u3059\u3002<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true \">let promise = new Promise(resolve =&gt; {\r\n  resolve(\"\u30c6\u30b9\u30c8\");\r\n})\r\n\r\nlet a = promise\r\nconsole.log(a); \/\/ Promise {}<\/pre>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u95a2\u6570\u306e\u524d\u306basync\u3001promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u524d\u306bawait\u3092\u3064\u3051\u308b\u4e8b\u3067<span style=\"color: #ff0000;\"><strong>then\u3092\u4f7f\u308f\u306a\u304f\u3066\u3082resolve\u306b\u3088\u308a\u8fd4\u3055\u308c\u305f\u5024\u3092\u53d6\u5f97\u3067\u304d\u308b<\/strong><\/span>\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true \">let promise = new Promise(resolve =&gt; {\r\n  resolve(\"\u30c6\u30b9\u30c8\");\r\n})\r\n\r\nasync function kansu() {\r\n  let a = await promise\r\n  console.log(a); \/\/\u30c6\u30b9\u30c8\r\n}\r\nkansu();<\/pre>\n<p>\u3053\u306e\u3088\u3046\u306basync\u3001await\u304c\u9069\u7528\u3055\u308c\u305f\u95a2\u6570\u306e\u3053\u3068\u3092<span style=\"color: #ff0000;\"><strong>\u975e\u540c\u671f\u95a2\u6570<\/strong><\/span>\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<h4>async function<\/h4>\n<ul>\n<li>Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059\u95a2\u6570\uff08\u975e\u540c\u671f\u95a2\u6570\uff09\u3068\u3044\u3046\u3053\u3068\u3092\u6307\u3057\u307e\u3059\u3002<\/li>\n<li>\u975e\u540c\u671f\u95a2\u6570\u306a\u3044\u3067return\u3059\u308b\u3068resolve\u3057\u305f\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<h4>await [Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059\u95a2\u6570]<\/h4>\n<ul>\n<li>\u5f15\u6570\u3067\u6307\u5b9a\u3057\u305f\u300cPromise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u3059\u95a2\u6570\u300d\u304cresolve\u3001reject\u3059\u308b\u306e\u3092\u5f85\u3061\u307e\u3059\u3002<\/li>\n<li>Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u8fd4\u3063\u3066\u6765\u308b\u306e\u3092\u5f85\u3063\u3066\u3044\u308b\u9593\u306f\u3053\u306e\u6587\u3088\u308a\u4e0b\u306e\u51e6\u7406\u306f\u5b9f\u884c\u3055\u308c\u307e\u305b\u3093\u3002<\/li>\n<\/ul>\n<h3>\u3088\u308a\u7c21\u6f54\u306b\u8a18\u8ff0\u3059\u308b\u3002<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b<span style=\"color: #ff0000;\"><strong>\u9759\u7684\u306aresolve\u30e1\u30bd\u30c3\u30c9<\/strong><\/span>\u3092\u4f7f\u3063\u305f\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u3066\u3088\u308a\u7c21\u6f54\u306b\u8a18\u8ff0\u3059\u308b\u4e8b\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let promise1 = Promise.resolve(\"\u30c6\u30b9\u30c8\");\r\nlet promise2 = Promise.resolve(\"\u30c6\u30b9\u30c82\");\r\n\r\nasync function kansu() {\r\n  let a = await promise1\r\n  let b = await promise2\r\n  console.log(a); \/\/\u30c6\u30b9\u30c8\r\n  console.log(b); \/\/\u30c6\u30b9\u30c82\r\n}\r\nkansu();<\/pre>\n<h3>\u30a8\u30e9\u30fc\u51e6\u7406<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>reject<\/strong><\/span>\u3092\u4f7f\u3063\u305f\u30a8\u30e9\u30fc\u51e6\u7406\u306f<span style=\"color: #ff0000;\"><strong>\u975e\u540c\u671f\u95a2\u6570\u5185\u3067<\/strong><strong>try \u301c catch<\/strong><strong>\u3067\u56f2\u3048\u3070\u5b9f\u73fe\u3059\u308b\u4e8b\u304c\u53ef\u80fd<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let promise1 = Promise.resolve(\"\u30c6\u30b9\u30c8\");\r\nlet promise2 = Promise.reject(\"\u30a8\u30e9\u30fc\u767a\u751f\");\r\n\r\nasync function kansu() {\r\n  try {\r\n    let a = await promise1\r\n    let b = await promise2\r\n    console.log(a);\r\n    console.log(b);\r\n  } catch (error) {\r\n    console.log(error); \/\/\u30a8\u30e9\u30fc\u767a\u751f\r\n  }\r\n}\r\nkansu();<\/pre>\n<h3>Promise.all<\/h3>\n<p>await\u3092\u9023\u7d9a\u3057\u3066\u8a18\u8ff0\u3059\u308b\u3068\u3044\u3046\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u304c\u3001<span style=\"color: #ff0000;\"><strong>Promise.all\u3092\u4f7f\u3046\u4e8b\u3067\u3088\u308a\u7c21\u6f54\u306b\u8a18\u8ff0\u3059\u308b\u4e8b\u304c\u53ef\u80fd<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let promise1 = Promise.resolve(\"\u30c6\u30b9\u30c8\");\r\nlet promise2 = Promise.resolve(\"\u30c6\u30b9\u30c82\");\r\n\r\nasync function kansu() {\r\n  let [a, b] = await Promise.all([promise1, promise2])\r\n  console.log(a); \/\/\u30c6\u30b9\u30c8\r\n  console.log(b); \/\/\u30c6\u30b9\u30c82\r\n}\r\nkansu();<\/pre>\n<h3>\u30a2\u30ed\u30fc\u95a2\u6570\u3092\u4f7f\u3063\u3066\u8a18\u8ff0\u3059\u308b\u3002<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u975e\u540c\u671f\u95a2\u6570\u306f\u30a2\u30ed\u30fc\u95a2\u6570\u3092\u4f7f\u3063\u3066\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let promise = () =&gt; new Promise(resolve =&gt; {\r\n  console.log(\"\u3042\");\r\n  resolve();\r\n})\r\nlet abc = async () =&gt; {\r\n  await Promise.all([\r\n    promise(),\r\n    promise()\r\n  ]);\r\n};\r\n\r\nabc();<\/pre>\n<h3>\u975e\u540c\u671f\u95a2\u6570\u304b\u3089\u975e\u540c\u671f\u95a2\u6570\u3092\u547c\u3076<\/h3>\n<p>\u975e\u540c\u671f\u95a2\u6570\u304b\u3089\u975e\u540c\u671f\u95a2\u6570\u3092\u547c\u3093\u3060\u5834\u5408\u3067\u3082\u3061\u3083\u3093\u3068\u9806\u756a\u306b\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let promise1 = Promise.resolve(\"\u30c6\u30b9\u30c8\");\r\nlet promise2 = Promise.resolve(\"\u30c6\u30b9\u30c82\");\r\nasync function kansu2() {\r\n  let a = await promise1;\r\n  let b = await promise2;\r\n  console.log(a);\r\n  console.log(b);\r\n}\r\nasync function kansu() {\r\n  console.log(\"\u3042\");\r\n  await kansu2();\r\n  console.log(\"\u3044\");\r\n}\r\nkansu();<\/pre>\n<h4>\u5b9f\u884c\u7d50\u679c<\/h4>\n<p>\u3061\u3083\u3093\u3068Promise\u306e\u7d50\u679c\u3092\u5f85\u3063\u3066\u9806\u756a\u306b\u51e6\u7406\u3055\u308c\u3066\u3044\u308b\u4e8b\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true  \">\u3042\r\n\u30c6\u30b9\u30c8\r\n\u30c6\u30b9\u30c82\r\n\u3044<\/pre>\n","protected":false},"excerpt":{"rendered":"async\u3001await\u3068\u306f\uff1f \u975e\u540c\u671f\u51e6\u7406\u3092\u8a18\u8ff0\u3067\u304d\u308b\u6587\u6cd5\u3067\u3001Promise\u3092\u3088\u308a\u7c21\u6f54\u306b\u8a18\u8ff0\u3059\u308b\u4e8b\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002Promise\u306b\u95a2\u3057\u3066\u306f\u4e0b\u8a18\u306e\u8a18\u4e8b\u3067\u89e3\u8aac\u3057\u3066\u304a\u308a\u307e\u3059\u3002 \u3010JavaScript\u3011\u975e\u540c\u671f\u51e6\u7406\u3092\u7c21\u5358\u306b\u3057\u3001\u30b3\u30fc [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/10312"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10312"}],"version-history":[{"count":14,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/10312\/revisions"}],"predecessor-version":[{"id":20285,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/10312\/revisions\/20285"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10312"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}