{"id":1389,"date":"2025-03-17T09:54:13","date_gmt":"2025-03-17T00:54:13","guid":{"rendered":"https:\/\/route-zero.com\/recruit\/?post_type=route&#038;p=1389"},"modified":"2025-07-17T09:54:47","modified_gmt":"2025-07-17T00:54:47","slug":"async%e9%96%a2%e6%95%b0%e3%81%a7then%e3%81%8c%e4%bd%bf%e3%81%88%e3%81%aa%e3%81%84%ef%bc%9f%e5%8e%9f%e5%9b%a0%e3%81%a8%e5%af%be%e7%ad%96%e3%82%92%e5%ae%8c%e5%85%a8%e8%a7%a3%e8%aa%ac","status":"publish","type":"route","link":"https:\/\/route-zero.com\/recruit\/route\/1389\/","title":{"rendered":"async\u95a2\u6570\u3067then\u304c\u4f7f\u3048\u306a\u3044\uff1f\u539f\u56e0\u3068\u5bfe\u7b56\u3092\u5b8c\u5168\u89e3\u8aac"},"content":{"rendered":"<div>\n<h2>\u3010TypeError\u5bfe\u7b56\u3011async\u95a2\u6570\u306bthen\u304c\u4f7f\u3048\u306a\u3044\uff1f\u3088\u304f\u3042\u308b\u843d\u3068\u3057\u7a74\u3068\u89e3\u6c7a\u6cd5<\/h2>\n<p>`then is not a function` \u306f\u3001\u975e\u540c\u671f\u51e6\u7406\u306e\u521d\u5b66\u8005\u304c\u6700\u3082\u3064\u307e\u305a\u304f\u30a8\u30e9\u30fc\u306e\u4e00\u3064\u3002Promise \u3084 async\/await \u306e\u610f\u5473\u3068\u5f79\u5272\u304b\u3089\u3001\u578b\u30c1\u30a7\u30c3\u30af\u30fb\u9632\u6b62\u7b56\u307e\u3067\u3092\u56f3\u89e3\uff06\u5b9f\u4f8b\u3064\u304d\u3067\u89e3\u8aac\uff01<\/p>\n<h2>async\/await \u3067\u300cthen is not a function\u300d\u306b\u906d\u9047\u3057\u305f\u77ac\u9593<\/h2>\n<p>\u975e\u540c\u671f\u51e6\u7406\u3067 <code>.then()<\/code> \u3092\u4f7f\u3063\u3066\u3044\u305f\u3089\u3001\u3042\u308b\u65e5\u3053\u3046\u3044\u3046\u30a8\u30e9\u30fc\u306b\u51fa\u304f\u308f\u3057\u305f\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u304b\uff1f<\/p>\n<pre><code>TypeError: resolveError(...).then is not a function<\/code><\/pre>\n<p>\u4e00\u898b\u300cPromise \u3063\u307d\u3044\u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u305f\u3064\u3082\u308a\u300d\u306a\u306e\u306b\u3001<code>.then()<\/code> \u304c\u4f7f\u3048\u306a\u3044\u2026\u3002\u3053\u306e\u30a8\u30e9\u30fc\u3001<strong>\u975e\u540c\u671f\u51e6\u7406\u306e\u69cb\u9020\u306b\u6f5c\u3080\u201c\u601d\u3044\u8fbc\u307f\u201d<\/strong>\u304c\u539f\u56e0\u3067\u3059\u3002<\/p>\n<p>\u672c\u8a18\u4e8b\u3067\u306f\u4ee5\u4e0b\u3092\u9806\u306b\u89e3\u8aac\u3057\u3001<strong>\u7406\u89e3 \u2192 \u4fee\u6b63 \u2192 \u518d\u767a\u9632\u6b62<\/strong>\u306e\u529b\u3092\u3064\u3051\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li>Promise\u30fbasync\u30fbawait \u3068\u306f\u4f55\u304b\uff1f\uff08\u7528\u8a9e\u7406\u89e3\uff09<\/li>\n<li>\u3088\u304f\u3042\u308b\u30a8\u30e9\u30fc\u306e\u30d1\u30bf\u30fc\u30f3\u3068\u69cb\u9020<\/li>\n<li>\u5b89\u5168\u306a\u4f7f\u3044\u65b9\u3068 Lint\u30fb\u578b\u30fb\u30c6\u30b9\u30c8\u306b\u3088\u308b\u9632\u5fa1\u7b56<\/li>\n<\/ul>\n<hr>\n<h2>Promise \u3068\u306f\uff1f \u2014 \u975e\u540c\u671f\u51e6\u7406\u306e\u300c\u672a\u6765\u306e\u7d50\u679c\u300d<\/h2>\n<p><strong>Promise\uff08\u30d7\u30ed\u30df\u30b9\uff09<\/strong>\u3068\u306f\u3001JavaScript \u306e\u975e\u540c\u671f\u51e6\u7406\u3092\u6271\u3046\u305f\u3081\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u3059\u3002<\/p>\n<p>\u7c21\u5358\u306b\u8a00\u3048\u3070\u300c<strong>\u3053\u308c\u304b\u3089\u5b8c\u4e86\u3059\u308b\u51e6\u7406\u306e\u7d50\u679c\u3092\u8868\u3059\u7bb1<\/strong>\u300d\u306e\u3088\u3046\u306a\u3082\u306e\u3002<\/p>\n<pre><code>const promise = new Promise((resolve, reject) =&gt; {\r\n  setTimeout(() =&gt; resolve('\u5b8c\u4e86\uff01'), 1000);\r\n});<\/code><\/pre>\n<ul>\n<li><code>.then()<\/code>\uff1a\u6210\u529f\u6642\u306e\u51e6\u7406<\/li>\n<li><code>.catch()<\/code>\uff1a\u5931\u6557\u6642\u306e\u51e6\u7406<\/li>\n<li><code>.finally()<\/code>\uff1a\u5b8c\u4e86\u5f8c\u306b\u5fc5\u305a\u5b9f\u884c<\/li>\n<\/ul>\n<pre><code>promise\r\n  .then((result) =&gt; console.log(result)) \/\/ =&gt; \"\u5b8c\u4e86\uff01\"\r\n  .catch((err) =&gt; console.error(err));<\/code><\/pre>\n<hr>\n<h2>async\/await \u3068\u306f\uff1fPromise \u3092\u3082\u3063\u3068\u8aad\u307f\u3084\u3059\u304f\u3059\u308b\u4ed5\u7d44\u307f<\/h2>\n<h3>async \u3068\u306f\uff1f<\/h3>\n<p><code>async<\/code> \u3092\u3064\u3051\u305f\u95a2\u6570\u306f\u3001<strong>\u5e38\u306b Promise \u3092\u8fd4\u3059\u95a2\u6570<\/strong>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code>async function greet() {\r\n  return 'Hello';\r\n}\r\ngreet().then((msg) =&gt; console.log(msg)); \/\/ \"Hello\"<\/code><\/pre>\n<h3>await \u3068\u306f\uff1f<\/h3>\n<p><code>await<\/code> \u306f\u3001Promise \u304c\u300c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u5f85\u3063\u3066\u304b\u3089\u300d\u6b21\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u69cb\u6587\u3067\u3059\u3002<\/p>\n<pre><code>async function run() {\r\n  const msg = await greet();\r\n  console.log(msg); \/\/ \"Hello\"\r\n}<\/code><\/pre>\n<h3>async\/await \u306e\u5229\u70b9<\/h3>\n<ul>\n<li><code>.then().then()<\/code> \u306e\u30cd\u30b9\u30c8\u3092\u907f\u3051\u3066\u3001<strong>\u540c\u671f\u51e6\u7406\u306e\u3088\u3046\u306b\u30b9\u30c3\u30ad\u30ea\u66f8\u3051\u308b<\/strong><\/li>\n<li><code>try\/catch<\/code> \u3067\u30a8\u30e9\u30fc\u51e6\u7406\u304c\u660e\u78ba\u306b\u306a\u308b<\/li>\n<li>\u975e\u540c\u671f\u51e6\u7406\u3092<strong>\u5206\u304b\u308a\u3084\u3059\u304f\u69cb\u9020\u5316<\/strong>\u3067\u304d\u308b<\/li>\n<\/ul>\n<hr>\n<h2>\u30b1\u30fc\u30b9 1\uff1aasync \u306a\u3057 \u2192 Promise \u3058\u3083\u306a\u3044\u623b\u308a\u5024\u306b.then()\u3057\u3066 TypeError<\/h2>\n<h3>\u30c0\u30e1\u306a\u30b3\u30fc\u30c9\u4f8b<\/h3>\n<pre><code>function fetchData() {\r\n  return { data: 'hello' };\r\n}\r\nfetchData().then((res) =&gt; {\r\n  console.log(res.data); \/\/ TypeError\r\n});<\/code><\/pre>\n<h3>\u4fee\u6b63\u4f8b<\/h3>\n<pre><code>async function fetchData() {\r\n  return { data: 'hello' };\r\n}\r\nfetchData().then((res) =&gt; {\r\n  console.log(res.data);\r\n});\r\n\/\/ await \u306e\u5834\u5408\r\nasync function main() {\r\n  const res = await fetchData();\r\n  console.log(res.data);\r\n}<\/code><\/pre>\n<h3>\u30c7\u30d0\u30c3\u30b0 Tips<\/h3>\n<pre><code>const result = fetchData();\r\nif (typeof result.then === 'function') {\r\n  result.then((res) =&gt; console.log(res));\r\n} else {\r\n  console.warn('Promise\u3058\u3083\u306a\u3044\u306e\u3067.then()\u4e0d\u53ef');\r\n}<\/code><\/pre>\n<hr>\n<h2>\u30b1\u30fc\u30b9 2\uff1aawait \u5fd8\u308c \u2192 undefined.then()\u306e\u843d\u3068\u3057\u7a74<\/h2>\n<h3>\u30c0\u30e1\u306a\u30b3\u30fc\u30c9\u4f8b<\/h3>\n<pre><code>async function getUser() {\r\n  return { name: 'Alice' };\r\n}\r\nasync function main() {\r\n  const user = getUser(); \/\/ await \u5fd8\u308c\r\n  user.then((res) =&gt; {\r\n    console.log(res.name);\r\n  });\r\n}<\/code><\/pre>\n<h3>\u4fee\u6b63\u4f8b<\/h3>\n<pre><code>async function main() {\r\n  const user = await getUser();\r\n  console.log(user.name);\r\n}<\/code><\/pre>\n<h3>Lint \u3067\u306e\u9632\u6b62\u30eb\u30fc\u30eb<\/h3>\n<pre><code>\"rules\": {\r\n  \"require-await\": \"error\",\r\n  \"no-return-await\": \"error\"\r\n}<\/code><\/pre>\n<hr>\n<h2>async\/await \u3068 Promise \u306e\u95a2\u4fc2\u3092\u56f3\u89e3\u3067\u6574\u7406<\/h2>\n<pre><code>[\u540c\u671f\u95a2\u6570]         \u2192 \u5024\uff08\u305f\u3060\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff09      \u2192 .then() \u00d7\r\n[async\u95a2\u6570]        \u2192 Promise&lt;\u5024&gt;                  \u2192 .then() \u3007\r\n[await\u4ed8\u304dasync]   \u2192 \u5024\uff08Promise\u306e\u4e2d\u8eab\uff09          \u2192 .then() \u00d7\uff08\u4e0d\u8981\uff09<\/code><\/pre>\n<ul>\n<li>async \u95a2\u6570\u306f\u81ea\u52d5\u7684\u306b <code>Promise&lt;\u623b\u308a\u5024&gt;<\/code> \u3092\u8fd4\u3059<\/li>\n<li><code>.then()<\/code> \u3092\u4f7f\u3048\u308b\u306e\u306f Promise \u306b\u5bfe\u3057\u3066\u3060\u3051<\/li>\n<li>await \u3092\u4f7f\u3046\u3053\u3068\u3067 <code>.then()<\/code> \u3092\u4f7f\u308f\u306a\u304f\u3066\u3082\u4e2d\u8eab\u304c\u53d6\u308a\u51fa\u305b\u308b<\/li>\n<\/ul>\n<hr>\n<h2>\u518d\u767a\u9632\u6b62\uff06\u8a2d\u8a08\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h2>\n<h3>ESLint \u8a2d\u5b9a\u4f8b<\/h3>\n<pre><code>\"rules\": {\r\n  \"no-misused-promises\": \"error\",\r\n  \"consistent-return\": \"warn\"\r\n}<\/code><\/pre>\n<h3>TypeScript \u306e\u578b\u30c1\u30a7\u30c3\u30af<\/h3>\n<pre><code>async function getValue(): Promise&lt;string&gt; {\r\n  return 'hello';\r\n}\r\nconst value: string = await getValue();<\/code><\/pre>\n<h3>Jest \u30c6\u30b9\u30c8\u4f8b<\/h3>\n<pre><code>test('Promise resolves correctly', async () =&gt; {\r\n  const result = await getValue();\r\n  expect(result).toBe('hello');\r\n});<\/code><\/pre>\n<hr>\n<h2>\u307e\u3068\u3081 &#038; \u95a2\u9023\u30ea\u30f3\u30af<\/h2>\n<ul>\n<li><strong>.then is not a function<\/strong> \u306f\u300cPromise \u3092\u8fd4\u3057\u3066\u3044\u306a\u3044\u300d\u300cawait \u3092\u5fd8\u308c\u3066\u3044\u308b\u300d\u306a\u3069\u304c\u539f\u56e0<\/li>\n<li>async\/await\/Promise \u306e\u610f\u5473\u3068\u5f79\u5272\u3092\u77e5\u308b\u3053\u3068\u304c\u6839\u672c\u5bfe\u7b56<\/li>\n<li>Lint\u30fb\u578b\u30c1\u30a7\u30c3\u30af\u30fb\u30c6\u30b9\u30c8\u3067\u518d\u767a\u3092\u672a\u7136\u306b\u9632\u3054\u3046<\/li>\n<\/ul>\n<h3>\u3010\u5916\u90e8\u30ea\u30f3\u30af\u3011<\/h3>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Promise\">MDN Web Docs: Promise<\/a><\/p>\n<p><a href=\"https:\/\/eslint.org\/docs\/latest\/rules\/require-await\">ESLint: require-await \u30eb\u30fc\u30eb<\/a><\/p>\n<h3>\u3010\u5185\u90e8\u30ea\u30f3\u30af\u3011<\/h3>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1109\/\">JavaScript\u3067\u3088\u304f\u767a\u751f\u3059\u308b\u30a8\u30e9\u30fc\u300cReferenceError\u300d\u300cSyntaxError\u300d\u306a\u3069\u306e\u539f\u56e0\u3068\u5bfe\u7b56\u3092\u5177\u4f53\u4f8b\u4ed8\u304d\u3067\u89e3\u8aac<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1312\/\">TypeError: Cannot read property &#8216;xxx&#8217; of undefined \u306e\u539f\u56e0\u3068\u5bfe\u51e6\u6cd5\u3092\u5fb9\u5e95\u89e3\u8aac\u3010\u521d\u5fc3\u8005\u5411\u3051\u3011<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1340\/\">\u306a\u305c\u300cundefined\u300d\u304c\u51fa\u308b\uff1f\u539f\u56e0\u3068\u89e3\u6c7a\u6cd510\u9078\uff5cJavaScript \u306e\u30c7\u30d0\u30c3\u30b0\u306b\u52b9\u304f\u5b9f\u4f8b\u96c6<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1212\/\">\u975e\u540c\u671f\u51e6\u7406\u3068\u306f\uff1fJavaScript\u3068Java\u306e\u5b9f\u4f8b\u3067\u308f\u304b\u308b\u4f7f\u3044\u65b9\u30fb\u843d\u3068\u3057\u7a74\u30fb\u89e3\u6c7a\u6cd5<\/a><\/p>\n<p><a href=\"https:\/\/route-zero.com\/recruit\/route\/1128\/\">JavaScript\u306eAjax\u5b8c\u5168\u5165\u9580\uff5cfetch\u306e\u4f7f\u3044\u65b9\u30fbCORS\u30a8\u30e9\u30fc\u306e\u5bfe\u51e6\u30fb\u975e\u540c\u671f\u901a\u4fe1\u306e\u57fa\u672c\u3092\u89e3\u8aac<\/a><\/p>\n<\/div>\n","protected":false},"featured_media":1391,"template":"","_links":{"self":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route\/1389"}],"collection":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route"}],"about":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/types\/route"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media\/1391"}],"wp:attachment":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media?parent=1389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}