{"id":53354,"date":"2024-12-03T14:02:53","date_gmt":"2024-12-03T06:02:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53354\/"},"modified":"2024-12-03T14:02:53","modified_gmt":"2024-12-03T06:02:53","slug":"javascript-promise%e5%90%8c%e6%ad%a5%e8%b0%83%e7%94%a8%ef%bc%9aawait%e4%b8%80%e4%b8%aa%e6%9c%aaresolve-reject%e7%9a%84promise%e4%bc%9a%e5%8f%91%e7%94%9f%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53354\/","title":{"rendered":"JavaScript Promise\u540c\u6b65\u8c03\u7528\uff1aawait\u4e00\u4e2a\u672aresolve\/reject\u7684Promise\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>JavaScript Promise\u540c\u6b65\u8c03\u7528\uff1aawait\u4e00\u4e2a\u672aresolve\/reject\u7684Promise\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aJavaScript Promise\u540c\u6b65\u8c03\u7528\uff1aawait\u4e00\u4e2a\u672aresolve\/reject\u7684Promise\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/17328724616749890db6ca9.jpg\" class=\"aligncenter\" title=\"JavaScript Promise\u540c\u6b65\u8c03\u7528\uff1aawait\u4e00\u4e2a\u672aresolve\/reject\u7684Promise\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u63d2\u56fe\" alt=\"JavaScript Promise\u540c\u6b65\u8c03\u7528\uff1aawait\u4e00\u4e2a\u672aresolve\/reject\u7684Promise\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript promise \u540c\u6b65\u8c03\u7528\u4e2d\u7684\u56f0\u60d1<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\u4f7f\u7528 promise \u65f6\uff0c\u6709\u65f6\u5019\u4f1a\u51fa\u73b0\u770b\u4f3c\u540c\u6b65\u8c03\u7528\u7684\u60c5\u51b5\uff0c\u5bfc\u81f4\u4ee4\u4eba\u56f0\u60d1\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u5728\u4e0b\u5217\u4ee3\u7801\u4e2d\uff1a<\/p>\n<pre>async function yyy() {\n  console.log('yyy\u8c03\u7528xxx\u5f00\u59cb');\n  let res = await xxx();\n  console.log('yyy\u8c03\u7528xxx\u7ed3\u675f', res);\n}\n\nasync function xxx() {\n  return new promise(function (resolve, reject) {});\n}<\/pre>\n<p>\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u51fd\u6570 xxx \u4e2d\u6ca1\u6709\u8c03\u7528 resolve \u6216 reject\uff0c\u90a3\u4e48\u51fd\u6570 yyy \u5728\u8fd0\u884c\u5230 await xxx() \u65f6\u4f1a\u7ed3\u675f\u8fd0\u884c\u8fd8\u662f\u4e00\u76f4\u963b\u585e\uff1f<\/li>\n<li>\u6ca1\u6709\u6267\u884c console.log(&#8216;yyy\u8c03\u7528xxx\u7ed3\u675f&#8217;, res)\uff0c\u7a0b\u5e8f\u662f\u81ea\u52a8\u7ed3\u675f\u4e86\u8fd8\u662f\u963b\u585e\u4e2d\uff1f<\/li>\n<\/ol>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>\u7b54\u6848\u56e0\u8fd0\u884c\u73af\u5883\u800c\u5f02\uff1a<\/p>\n<p><strong>node.js<\/strong><\/p>\n<p>\u5728 node.js \u4e2d\uff0c\u8fd4\u56de\u7684 promise \u6c38\u8fdc\u5904\u4e8e\u6302\u8d77\u72b6\u6001\uff0c\u56e0\u4e3a\u6ca1\u6709\u88ab resolved \u6216 rejected\u3002\u4f46\u662f\uff0c\u7531\u4e8e\u6ca1\u6709\u5176\u4ed6\u6d3b\u8dc3\u7684\u5f02\u6b65\u64cd\u4f5c\uff0cnode.js \u7684\u4e8b\u4ef6\u5faa\u73af\u4f1a\u8ba4\u4e3a\u7a0b\u5e8f\u5df2\u5b8c\u6210\uff0c\u5bfc\u81f4\u8fdb\u7a0b\u9000\u51fa\u3002<\/p>\n<p><strong>\u6d4f\u89c8\u5668<\/strong><\/p>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\uff0cawait \u4f1a\u4e00\u76f4\u7b49\u5f85 promise \u5b8c\u6210\u3002\u5982\u679c\u6ca1\u6709\u5176\u4ed6\u5f02\u6b65\u64cd\u4f5c\uff0c\u6d4f\u89c8\u5668\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u811a\u672c\u5df2\u6267\u884c\u5b8c\u6bd5\uff0c\u4f46\u4f60\u7684\u811a\u672c\u7684\u5269\u4f59\u90e8\u5206\u5c06\u4e0d\u4f1a\u6267\u884c\u3002\u7528\u6237\u4ecd\u7136\u53ef\u4ee5\u4e0e\u9875\u9762\u4ea4\u4e92\uff0c\u4f46\u4f60\u7684\u811a\u672c\u4e0d\u4f1a\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5c06 xxx \u51fd\u6570\u4fee\u6539\u4e3a\u4e00\u4e2a\u59cb\u7ec8\u8fd4\u56de resolved promise \u7684\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>async function xxx() {\n  return new Promise((resolve, reject) =&gt; {\n    resolve('\u5df2\u5b8c\u6210');\n  });\n}<\/pre>\n<p>\u8fd9\u6837\uff0cyyy \u51fd\u6570\u5c06\u59cb\u7ec8\u6267\u884c console.log(&#8216;yyy\u8c03\u7528xxx\u7ed3\u675f&#8217;, res)\uff0c\u5e76\u4e14\u4e0d\u4f1a\u963b\u585e\u811a\u672c\u6267\u884c\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aJavaScript Promise\u540c\u6b65\u8c03\u7528\uff1aawait\u4e00\u4e2a\u672aresolve\/reject\u7684Promise\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Visual Studio \u80fd\u7f16\u5199 GoLang \u9879\u76ee\u5417\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Promise\u540c\u6b65\u8c03\u7528\uff1aawait\u4e00\u4e2a\u672aresolve\/reject\u7684Promise\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f \u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aJavaScript Promise\u540c\u6b65\u8c03\u7528\uff1aawait\u4e00\u4e2a\u672aresolve\/reject\u7684Promise\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01 javascript promise \u540c\u6b65\u8c03\u7528\u4e2d\u7684\u56f0\u60d1 \u5728 javascript \u4e2d\u4f7f\u7528 promise \u65f6\uff0c\u6709\u65f6\u5019\u4f1a\u51fa\u73b0\u770b\u4f3c\u540c\u6b65\u8c03\u7528\u7684\u60c5\u51b5\uff0c\u5bfc\u81f4\u4ee4\u4eba\u56f0\u60d1\u7684\u7ed3\u679c\u3002 \u5728\u4e0b\u5217\u4ee3\u7801\u4e2d\uff1a async function yyy() { console.log(&#8216;yyy\u8c03\u7528xxx\u5f00\u59cb&#8217;); let res = await xxx(); console.log(&#8216;yyy\u8c03\u7528xxx\u7ed3\u675f&#8217;, res); } async function xxx() { return new promise(function (resolve, reject) {}); } \u95ee\u9898\uff1a \u51fd\u6570 xxx \u4e2d\u6ca1\u6709\u8c03\u7528 resolve \u6216 reject\uff0c\u90a3\u4e48\u51fd\u6570 yyy \u5728\u8fd0\u884c\u5230 await xxx() \u65f6\u4f1a\u7ed3\u675f\u8fd0\u884c\u8fd8\u662f\u4e00\u76f4\u963b\u585e\uff1f \u6ca1\u6709\u6267\u884c console.log(&#8216;yyy\u8c03\u7528xxx\u7ed3\u675f&#8217;, res)\uff0c\u7a0b\u5e8f\u662f\u81ea\u52a8\u7ed3\u675f\u4e86\u8fd8\u662f\u963b\u585e\u4e2d\uff1f \u56de\u7b54\uff1a \u7b54\u6848\u56e0\u8fd0\u884c\u73af\u5883\u800c\u5f02\uff1a node.js [&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-53354","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53354","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=53354"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53354\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}