{"id":52665,"date":"2024-12-03T14:12:10","date_gmt":"2024-12-03T06:12:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52665\/"},"modified":"2024-12-03T14:12:10","modified_gmt":"2024-12-03T06:12:10","slug":"react-%e4%b8%ad%e4%bd%bf%e7%94%a8-promise-%e6%97%b6%ef%bc%8c%e5%87%bd%e6%95%b0%e5%a4%a7%e6%8b%ac%e5%8f%b7%e7%9a%84%e5%bd%b1%e5%93%8d%e5%8e%9f%e7%90%86%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52665\/","title":{"rendered":"React \u4e2d\u4f7f\u7528 Promise \u65f6\uff0c\u51fd\u6570\u5927\u62ec\u53f7\u7684\u5f71\u54cd\u539f\u7406\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>React \u4e2d\u4f7f\u7528 Promise \u65f6\uff0c\u51fd\u6570\u5927\u62ec\u53f7\u7684\u5f71\u54cd\u539f\u7406\u662f\u4ec0\u4e48\uff1f <\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aReact \u4e2d\u4f7f\u7528 Promise \u65f6\uff0c\u51fd\u6570\u5927\u62ec\u53f7\u7684\u5f71\u54cd\u539f\u7406\u662f\u4ec0\u4e48\uff1f \u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/17322575976740273d9a820.jpg\" class=\"aligncenter\" title=\"React \u4e2d\u4f7f\u7528 Promise \u65f6\uff0c\u51fd\u6570\u5927\u62ec\u53f7\u7684\u5f71\u54cd\u539f\u7406\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"React \u4e2d\u4f7f\u7528 Promise \u65f6\uff0c\u51fd\u6570\u5927\u62ec\u53f7\u7684\u5f71\u54cd\u539f\u7406\u662f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>react promise \u4e2d\u52a0\/\u4e0d\u52a0\u51fd\u6570\u5927\u62ec\u53f7\u7684\u5f71\u54cd\u539f\u7406<\/strong><\/p>\n<p>\u5728 react \u4e2d\uff0c\u4f7f\u7528 promise \u8fdb\u884c\u5f02\u6b65\u64cd\u4f5c\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u5728\u51fd\u6570\u524d\u9762\u52a0\u4e0a\u6216\u4e0d\u52a0\u4e0a\u5927\u62ec\u53f7\u7684\u60c5\u51b5\u3002\u8fd9\u4e24\u79cd\u5199\u6cd5\u4ea7\u751f\u4e0d\u540c\u7684\u6548\u679c\uff0c\u8ba9\u4eba\u8ff7\u60d1\u3002<\/p>\n<p><strong>\u95ee\u9898\uff1a\u6709\u65e0\u5927\u62ec\u53f7\u7684\u533a\u522b<\/strong><\/p>\n<p>\u4e0b\u9762\u5c55\u793a\u4e86\u4e24\u79cd\u5199\u6cd5\u7684\u5bf9\u6bd4\uff1a<\/p>\n<p><strong>\u6b63\u786e\u5199\u6cd5\uff08\u65e0\u5927\u62ec\u53f7\uff09\uff1a<\/strong><\/p>\n<pre>get: () =&gt; new promise((resolve) =&gt; {...})<\/pre>\n<p><strong>\u9519\u8bef\u5199\u6cd5\uff08\u6709\u5927\u62ec\u53f7\uff09\uff1a<\/strong><\/p>\n<pre>get: () =&gt; { new promise((resolve) =&gt; {...}) }<\/pre>\n<p>\u8fd9\u4e24\u79cd\u5199\u6cd5\u7684\u533a\u522b\u5728\u4e8e\uff0c\u65e0\u5927\u62ec\u53f7\u7684\u5199\u6cd5\u5c06 new promise \u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u800c\u6709\u5927\u62ec\u53f7\u7684\u5199\u6cd5\uff0c\u4f1a\u5c06\u51fd\u6570\u8fdb\u4e00\u6b65\u5305\u88c5\u4e00\u5c42\uff0c\u5bfc\u81f4\u65e0\u6cd5\u8bbf\u95ee promise \u7684 resolve \u51fd\u6570\u3002<\/p>\n<p><strong>\u539f\u7406\u89e3\u91ca<\/strong><\/p>\n<p>\u5f53\u4f7f\u7528\u65e0\u5927\u62ec\u53f7\u65f6\uff0cget \u51fd\u6570\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a promise \u5b9e\u4f8b\u3002\u800c\u5f53\u6dfb\u52a0\u5927\u62ec\u53f7\u65f6\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u6b64\u51fd\u6570\u8fd4\u56de promise \u5b9e\u4f8b\u3002\u5728\u7b2c\u4e8c\u79cd\u5199\u6cd5\u4e2d\uff0c\u533f\u540d\u51fd\u6570\u7684\u8fd4\u56de\u503c\u88ab discard \u6389\uff0c\u65e0\u6cd5\u8bbf\u95ee resolve \u51fd\u6570\u3002\u56e0\u6b64\uff0cget \u51fd\u6570\u5b9e\u9645\u4e0a\u6ca1\u6709\u8fd4\u56de promise\uff0c\u5bfc\u81f4\u5f02\u6b65\u64cd\u4f5c\u5931\u8d25\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u5305\u542b\u8fd9\u4e24\u79cd\u5199\u6cd5\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>const getdata = async () =&gt; {\n  \/\/ ...\n}\n\nuseeffect(() =&gt; {\n  currentref.current = {\n    get: () =&gt; new promise((resolve) =&gt; { \/\/ \u6b63\u786e\u5199\u6cd5\n      getdata().then((res) =&gt; {\n        \/\/ ...\n        resolve({ ... });\n      }).catch((error) =&gt; {\n        if (error &amp;&amp; error.errorfields) {\n          \/\/ ...\n        }\n      });\n    })\n  };\n}, []);<\/pre>\n<pre>const getData = async () =&gt; {\n  \/\/ ...\n}\n\nuseEffect(() =&gt; {\n  currentRef.current = {\n    get: () =&gt; { \/\/ \u9519\u8bef\u5199\u6cd5\uff0c\u65e0\u6cd5\u8bbf\u95ee resolve \u51fd\u6570\n      new Promise((resolve) =&gt; {\n        getData().then((res) =&gt; {\n          \/\/ ...\n          resolve({ ... });\n        }).catch((error) =&gt; {\n          if (error &amp;&amp; error.errorFields) {\n            \/\/ ...\n          }\n        });\n      });\n    }\n  };\n}, []);<\/pre>\n<p>\u56e0\u6b64\uff0c\u5728 react \u4e2d\u4f7f\u7528 promise \u65f6\uff0c\u8bf7\u786e\u4fdd\u7701\u7565\u51fd\u6570\u5927\u62ec\u53f7\uff0c\u4ee5\u6b63\u786e\u8fd4\u56de promise \u5b9e\u4f8b\u5e76\u5b9e\u73b0\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u963f\u91cc\u5df4\u5df4\uff1a\u4e0e\u5fae\u4fe1\u652f\u4ed8\u5408\u4f5c\u7684\u6f5c\u5728\u7528\u6237\u589e\u91cf\u7a7a\u95f4\u5f88\u5927\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>React \u4e2d\u4f7f\u7528 Promise \u65f6\uff0c\u51fd\u6570\u5927\u62ec\u53f7\u7684\u5f71\u54cd\u539f\u7406\u662f\u4ec0\u4e48\uff1f \u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aReact \u4e2d\u4f7f\u7528 Promise \u65f6\uff0c\u51fd\u6570\u5927\u62ec\u53f7\u7684\u5f71\u54cd\u539f\u7406\u662f\u4ec0\u4e48\uff1f \u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01 react promise \u4e2d\u52a0\/\u4e0d\u52a0\u51fd\u6570\u5927\u62ec\u53f7\u7684\u5f71\u54cd\u539f\u7406 \u5728 react \u4e2d\uff0c\u4f7f\u7528 promise \u8fdb\u884c\u5f02\u6b65\u64cd\u4f5c\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u5728\u51fd\u6570\u524d\u9762\u52a0\u4e0a\u6216\u4e0d\u52a0\u4e0a\u5927\u62ec\u53f7\u7684\u60c5\u51b5\u3002\u8fd9\u4e24\u79cd\u5199\u6cd5\u4ea7\u751f\u4e0d\u540c\u7684\u6548\u679c\uff0c\u8ba9\u4eba\u8ff7\u60d1\u3002 \u95ee\u9898\uff1a\u6709\u65e0\u5927\u62ec\u53f7\u7684\u533a\u522b \u4e0b\u9762\u5c55\u793a\u4e86\u4e24\u79cd\u5199\u6cd5\u7684\u5bf9\u6bd4\uff1a \u6b63\u786e\u5199\u6cd5\uff08\u65e0\u5927\u62ec\u53f7\uff09\uff1a get: () =&gt; new promise((resolve) =&gt; {&#8230;}) \u9519\u8bef\u5199\u6cd5\uff08\u6709\u5927\u62ec\u53f7\uff09\uff1a get: () =&gt; { new promise((resolve) =&gt; {&#8230;}) } \u8fd9\u4e24\u79cd\u5199\u6cd5\u7684\u533a\u522b\u5728\u4e8e\uff0c\u65e0\u5927\u62ec\u53f7\u7684\u5199\u6cd5\u5c06 new promise \u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u800c\u6709\u5927\u62ec\u53f7\u7684\u5199\u6cd5\uff0c\u4f1a\u5c06\u51fd\u6570\u8fdb\u4e00\u6b65\u5305\u88c5\u4e00\u5c42\uff0c\u5bfc\u81f4\u65e0\u6cd5\u8bbf\u95ee promise \u7684 resolve \u51fd\u6570\u3002 \u539f\u7406\u89e3\u91ca \u5f53\u4f7f\u7528\u65e0\u5927\u62ec\u53f7\u65f6\uff0cget \u51fd\u6570\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a promise \u5b9e\u4f8b\u3002\u800c\u5f53\u6dfb\u52a0\u5927\u62ec\u53f7\u65f6\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u6b64\u51fd\u6570\u8fd4\u56de promise \u5b9e\u4f8b\u3002\u5728\u7b2c\u4e8c\u79cd\u5199\u6cd5\u4e2d\uff0c\u533f\u540d\u51fd\u6570\u7684\u8fd4\u56de\u503c\u88ab discard \u6389\uff0c\u65e0\u6cd5\u8bbf\u95ee resolve [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52665","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52665"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52665\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}