{"id":360,"date":"2025-01-23T16:33:47","date_gmt":"2025-01-23T08:33:47","guid":{"rendered":"https:\/\/ikesallows.top\/?p=360"},"modified":"2025-01-23T17:57:18","modified_gmt":"2025-01-23T09:57:18","slug":"%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b-%e5%bc%82%e6%ad%a5%e5%87%bd%e6%95%b0%e4%b8%8e%e5%b9%b6%e5%8f%91","status":"publish","type":"post","link":"https:\/\/ikesallows.top\/index.php\/2025\/01\/23\/%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b-%e5%bc%82%e6%ad%a5%e5%87%bd%e6%95%b0%e4%b8%8e%e5%b9%b6%e5%8f%91\/","title":{"rendered":"\u5f02\u6b65\u7f16\u7a0b-\u5f02\u6b65\u51fd\u6570\u4e0e\u5e76\u53d1"},"content":{"rendered":"\n<p class=\"has-medium-font-size wp-block-paragraph\">\u4e00.\u57fa\u672c\u6982\u5ff5<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-style-plain is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-small-font-size wp-block-paragraph\">\u5f02\u6b65\u51fd\u6570\u901a\u8fc7\u975e\u963b\u585e\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5e76\u53d1\u4efb\u52a1\uff0c\u4f7f\u7a0b\u5e8f\u5728\u7b49\u5f85\u67d0\u4e9b\u64cd\u4f5c\u5b8c\u6210\u65f6\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">1.1 \u963b\u585e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f20\u7edf\u7684\u7f16\u7a0b\u6a21\u578b\u6216\u8005\u8bf4\u8bed\u8a00\u6a21\u5f0f\u901a\u5e38\u662f\u540c\u6b65\u7684\uff0c\u4f8b\u5982\u57fa\u7840\u7684C\u8bed\u8a00Java\u5728\u9ad8\u6821\u671f\u95f4\u7684\u53d8\u6210\u5f00\u53d1\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e2a\u64cd\u4f5c\u90fd\u662f\u987a\u5e8f\u6267\u884c\u7684\uff0c\u8c03\u7528\u4e00\u4e2a\u64cd\u4f5c\u65f6\u7a0b\u5e8f\u5c06\u4f1a\u7b49\u5f85\u8fd9\u4e2a\u64cd\u4f5c\u5b8c\u6210\u540e\u624d\u4f1a\u53bb\u7ee7\u7eed\u6267\u884c\u540e\u540e\u7eed\u7684\u4efb\u52a1\u3002<strong>\u5c24\u5176\u662f\uff1a\u6587\u4ef6\u8bfb\u53d6\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u64cd\u4f5c<\/strong>\uff0c\u5982\u679c\u9047\u5230<em>I\/O<\/em>\u64cd\u4f5c\uff0c\u7a0b\u5e8f\u5c06\u4f1a\u53d1\u751f\u963b\u585e\uff0c\u76f4\u5230\u6267\u884c\u64cd\u4f5c\u5b8c\u6210\u540e\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f02\u6b65\u7f16\u7a0b\u7684\u6838\u5fc3\u601d\u60f3\u5219\u662f\uff1a\u6863\u7a0b\u5e8f\u53d1\u8d77\u4e86\u67d0\u4e2a\u53ef\u80fd\u9700\u8981\u5f88\u957f\u65f6\u95f4\u7b49\u5f85\u7684\u64cd\u4f5c\u65f6\uff0c\u4ed6\u4e0d\u4f1a\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u7684\u6267\u884c\uff0c\u800c\u662f\u8ba9\u8be5\u64cd\u4f5c\u5728\u540e\u53f0\u8fdb\u884c\uff0c\u5f53\u7ebf\u7a0b\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002\u5f53\u64cd\u4f5c\u5b8c\u6210\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u53d7\u5230\u901a\u77e5\uff08\u901a\u77e5\u7684\u901a\u5e38\u65b9\u5f0f\u4e3a <strong>\u56de\u8c03\u51fd\u6570 \u4e8b\u4ef6 Promise<\/strong>\u7b49)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.2 \u56de\u8c03\u51fd\u6570\u4e0e\u4e8b\u4ef6\u9a71\u52a8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f02\u6b65\u51fd\u6570\u6700\u5e38\u89c1\u7684\u5b8c\u6210\u901a\u77e5\u65b9\u5f0f\u5c31\u662f\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u4efb\u52a1\u3002 \u5f53\u67d0\u4e2a\u5f02\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210\u540e\uff0c\u7a0b\u5e8f\u4f1a\u8c03\u7528\u9884\u8bbe\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Javascript\u3001python\u3001Node.js\u90fd\u5927\u91cf\u4f7f\u7528\u4e86\u56de\u8c03\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5f02\u6b65<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.3 \u4e8b\u4ef6\u5faa\u73af\u4e0e\u4efb\u52a1\u961f\u5217<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5355\u7ebf\u7a0b\u4efb\u52a1\u7684\u6a21\u578b\u4e2d\uff0c\u4e8b\u4ef6\u5faa\u73af\u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u6838\u5fc3\u673a\u5236\uff0c\u4e8b\u4ef6\u5faa\u73af\u662f\u4e00\u79cd\u8c03\u5ea6\u673a\u5236\uff0c\u5b83\u4e0d\u65ad\u68c0\u67e5\u4efb\u52a1\u961f\u5217\u4e2d\u662f\u5426\u6709\u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\uff0c\u7a0b\u5e8f\u7684\u5f02\u6b65\u64cd\u4f5c\u4f1a\u628a\u4efb\u52a1\u6216\u56de\u8c03\u51fd\u6570\u653e\u5165\u8fd9\u4e2a\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u4e8b\u4ef6\u5faa\u73af\u5219\u4f1a\u4ece\u4efb\u52a1\u961f\u5217\u4e2d\u53d6\u51fa\u4efb\u52a1\u5e76\u6267\u884c\uff0c\u4fdd\u8bc1\u4e86\u8be5\u4efb\u52a1\u6267\u884c\u7684\u987a\u5e8f\u548c\u975e\u963b\u585e\u6027<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.4 Promise\u4e0easync\/await<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u56de\u8c03\u51fd\u6570\u65b9\u5f0f\u7684\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u5d4c\u5957\u56de\u8c03\u901a\u5e38\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u548c\u7406\u89e3\uff1a\uff08\u6bd4\u5982rubbish\u4e2d\u7684\u6218\u6597\uff0c\u4f7f\u7528\u4e86\u591a\u4e2asetTimeOut\u5d4c\u5957\uff0c\u6bcf\u4e2a\u5ef6\u8fdf\u6267\u884c\u53c8\u4f7f\u7528\u4e86\u53d8\u91cf\u4e0e\u5e38\u91cf\u6df7\u5408\u7684\u65b9\u5f0f\u8fdb\u884c\u63a7\u5236\uff0c\u5f88\u591a\u65f6\u5019\u6839\u672c\u4e0d\u77e5\u9053\u662f\u7a0b\u5e8f\u54ea\u91cc\u7ed9\u8bbe\u7f6e\u4e86\u72b6\u6001\u6216\u53d8\u91cf)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u73b0\u4ee3\u8bed\u8a00\u5f15\u5165\u4e86Promise(\u627f\u8bfa)\u6a21\u5f0f\u3002Promise\u4ee3\u8868\u4e86\u4e00\u4e2a\u53ef\u80fd\u5728\u672a\u6765\u67d0\u4e2a\u65f6\u523b\u5b8c\u6210\u7684\u5f02\u6b65\u64cd\u4f5c\uff0c\u901a\u8fc7then()\u65b9\u6cd5\u53ef\u4ee5\u8bbe\u7f6e\u56de\u8c03\uff0c\u94fe\u5f0f\u8c03\u7528\u51cf\u5c11\u4e86\u56de\u8c03\u7684\u5c42\u7ea7\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">async\/await\u662f\u57fa\u4e8e\u627f\u8bfa\u6a21\u5f0fPromise\u7684<strong>\u8bed\u6cd5\u7cd6<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-small-font-size is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-small-font-size wp-block-paragraph\">\u8bed\u6cd5\u7cd6\uff0c\u4e5f\u8bd1\u4e3a\u7cd6\u8863\u8bed\u6cd5\uff0c\u662f\u7531\u82f1\u56fd\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\u5f7c\u5f97\u00b7\u5170\u4e01\u53d1\u660e\u7684\u4e00\u4e2a\u672f\u8bed\uff0c\u6307\u8ba1\u7b97\u673a\u8bed\u8a00\u4e2d\u6dfb\u52a0\u7684\u67d0\u79cd\u8bed\u6cd5\uff0c\u8fd9\u79cd\u8bed\u6cd5\u5bf9\u8bed\u8a00\u7684\u529f\u80fd\u6ca1\u6709\u5f71\u54cd\uff0c\u4f46\u662f\u66f4\u65b9\u4fbf\u7a0b\u5e8f\u5458\u4f7f\u7528\u3002\u8bed\u6cd5\u7cd6\u8ba9\u7a0b\u5e8f\u66f4\u52a0\u7b80\u6d01\uff0c\u6709\u66f4\u9ad8\u7684\u53ef\u8bfb\u6027\u3002\u201c\u8bed\u6cd5\u7cd6\u201d\u8fd9\u4e2a\u8bcd\u7edd\u975e\u8d2c\u4e49\u8bcd\uff0c\u5b83\u53ef\u4ee5\u7ed9\u6211\u5e26\u6765\u65b9\u4fbf\uff0c\u662f\u4e00\u79cd\u4fbf\u6377\u7684\u5199\u6cd5\uff0c\u7f16\u8bd1\u5668\u4f1a\u5e2e\u6211\u4eec\u505a\u8f6c\u6362\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u7f16\u7801\u7684\u6548\u7387\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e3\u91ca\uff1a\u5c31\u50cf\u662f\u4e58\u6cd5\u662f\u52a0\u6cd5\u7684\u9ad8\u7ea7\u5199\u6cd5\uff0c\u8fdb\u884c\u7684\u8fd0\u7b97\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u53ef\u662f\u5199\u6cd5\u4e0d\u540c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8db3\uff1a\u8bed\u6cd5\u7cd6\u6709\u7684\u65f6\u5019\u662f\u964d\u4f4e\u4e86\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u6765\u6362\u53d6\u6548\u7387\uff0c\u4e0d\u7ba1\u662f\u5199\u7a0b\u5e8f\u7684\u6548\u7387\u8fd8\u662f\u7a0b\u5e8f\u8fd0\u884c\u7684\u6548\u7387<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/ikesallows.top\/index.php\/2025\/01\/23\/%e8%af%ad%e6%b3%95%e7%b3%96%e3%80%81%e8%af%ad%e6%b3%95%e7%9b%90%e3%80%81%e8%af%ad%e6%b3%95%e7%b3%96%e7%b2%be%e3%80%81%e8%af%ad%e6%b3%95%e5%92%b3%e7%bd%97%e4%bc%8a\/\" data-type=\"post\" data-id=\"368\">\u8bed\u6cd5\u7cd6\u3001\u8bed\u6cd5\u76d0\u3001\u8bed\u6cd5\u7cd6\u7cbe\u3001\u8bed\u6cd5\u54b3\u7f57\u4f0a<\/a><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ed6\u4f7f\u5f97\u5f02\u6b65\u4ee3\u7801\u770b\u8d77\u6765\u50cf\u540c\u6b65\u4ee3\u7801\uff0c\u6781\u5927\u7684\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7await\u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c\u7684\u5b8c\u6210\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u963b\u585e\uff0c\u4f46\u4ee3\u7801\u7ed3\u6784\u5374\u4fdd\u6301\u4e86\u540c\u6b65\u7684\u5f62\u5f0f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662fansync\u548cawait<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9a\u4e49<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>async \u662f\u5f02\u6b65\u7684\u610f\u601d\uff0cawait\u5219\u53ef\u4ee5\u7406\u89e3\u4e3a async wait\u3002\u6240\u4ee5\u53ef\u4ee5\u7406\u89e3async\u5c31\u662f\u7528\u6765\u58f0\u660e\u4e00\u4e2a\u5f02\u6b65\u65b9\u6cd5\uff0c\u800c await\u662f\u7528\u6765\u7b49\u5f85\u5f02\u6b65\u65b9\u6cd5\u6267\u884c<br><\/li>\n\n\n\n<li>async\u4f5c\u4e3a\u4e00\u4e2a\u5173\u952e\u5b57\u653e\u5728\u51fd\u6570\u524d\u9762\uff0c\u8868\u793a\u8be5\u51fd\u6570\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u5f02\u6b65\u51fd\u6570\u610f\u5473\u7740\u8be5\u51fd\u6570\u7684\u6267\u884c\u4e0d\u4f1a\u963b\u585e\u540e\u9762\u4ee3\u7801\u7684\u6267\u884c\uff1b\u800c await \u7528\u4e8e\u7b49\u5f85\u4e00\u4e2a\u5f02\u6b65\u65b9\u6cd5\u6267\u884c\u5b8c\u6210\uff1b<br><\/li>\n\n\n\n<li>await \u7b49\u5f85\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u5982\u679c Promise\u7684\u72b6\u6001\u53d8\u6210\u4e86 resolve \u6216\u8005 rejcet\uff0c\u90a3\u4e48 async\u51fd\u6570\u4f1a\u6062\u590d\u6267\u884c\u3002\u5e76\u4f1a\u963b\u585e\u8be5\u51fd\u6570\u5185\u540e\u9762\u7684\u4ee3\u7801\u3002<br><\/li>\n\n\n\n<li>\u4f7f\u7528 async\/await \u53ef\u4ee5\u5b9e\u73b0\u7528\u540c\u6b65\u4ee3\u7801\u7684\u98ce\u683c\u6765\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\uff0c\u8fd9\u662f\u56e0\u4e3a async\/await \u7684\u57fa\u7840\u6280\u672f\u4f7f\u7528\u4e86\u751f\u6210\u5668\u548c Promise\uff0c\u751f\u6210\u5668\u662f\u534f\u7a0b\u7684\u5b9e\u73b0\uff0c\u5229\u7528\u751f\u6210\u5668\u80fd\u5b9e\u73b0\u751f\u6210\u5668\u51fd\u6570\u7684\u6682\u505c\u548c\u6062\u590d\u3002<br><\/li>\n\n\n\n<li>\u4e3a\u4e86\u4f18\u5316 .then \u94fe\u800c\u5f00\u53d1\u51fa\u6765\u7684\u3002<br><br><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">\u4e8c.\u5355\u7ebf\u7a0b\u5f02\u6b65\u53d8\u6210-\u4e8b\u4ef6\u5faa\u73af\u4e0e\u591a\u4efb\u52a1\u534f\u4f5c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2.1\u5355\u7ebf\u7a0b\u5f02\u6b65\u6a21\u578b\u7684\u4f18\u52bf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bb8\u591a\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\uff08\u5c24\u5176\u662fNode.js)\u90fd\u91c7\u7528\u4e86\u7684\u5355\u7ebf\u7a0b\u5f02\u6b65\u7684\u6a21\u578b\uff0c\u5728\u8fd9\u79cd\u6a21\u578b\u4e0b\uff0c\u53ea\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\u8d1f\u8d23\u6240\u6709\u4efb\u52a1\u7684\u8c03\u5ea6\u548c\u6267\u884c\uff0c\u901a\u8fc7\u975e\u963b\u585eI\/O\u5355\u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u4efb\u52a1\uff0c\u800c\u4e0d\u9700\u8981\u4e3a\u6bcf\u4e2a\u4efb\u52a1\u5355\u72ec\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\uff0c\u8fd9\u907f\u514d\u4e86\u53bf\u57ce\u5207\u6362\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u5f00\u9500\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u5728Node.js\u4e2d\uff0c\u6240\u6709\u7684I\/O\u64cd\u4f5c\uff08\u5982\u6587\u4ef6\u8bfb\u53d6\u3001\u6570\u636e\u5e93\u67e5\u8be2\u3001HTTP\u8bf7\u6c42\u7b49\uff09\u90fd\u4ee5\u5f02\u6b65\u65b9\u5f0f\u6267\u884c\u3002\u5f53\u4e00\u4e2a\u64cd\u4f5c\u88ab\u53d1\u8d77\u65f6\uff0c\u4e8b\u4ef6\u5faa\u73af\u4f1a\u5c06\u5b83\u4ea4\u7ed9\u5e95\u5c42\u64cd\u4f5c\u7cfb\u7edf\u6216\u5176\u4ed6\u4e13\u95e8\u7684\u7ebf\u7a0b\u6c60\u5904\u7406\uff0c\u800cNode.js\u4e3b\u7ebf\u7a0b\u5219\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u907f\u514d\u4e86\u56e0\u4e3a\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u800c\u5bfc\u81f4\u7684\u7a7a\u95f2\u65f6\u95f4\u6d6a\u8d39\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2.2\u4e8b\u4ef6\u7684\u5faa\u73af\u673a\u5236<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Node.js\u7684\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\u662f\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u7684\u5173\u952e\u3002\u5b83\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u9636\u6bb5\u6765\u5904\u7406\u5f02\u6b65\u4efb\u52a1\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Timers\uff1a\u6267\u884c\u5728setTimeout\u6216setInterval\u4e2d\u6ce8\u518c\u7684\u56de\u8c03\u3002<\/li>\n\n\n\n<li>I\/O callbacks\uff1a\u5904\u7406\u4e00\u4e9b\u5e38\u89c1\u7684\u7cfb\u7edf\u64cd\u4f5c\u7684\u56de\u8c03\uff08\u5982TCP\u8fde\u63a5\u3001\u6587\u4ef6I\/O\u7b49\uff09\u3002<\/li>\n\n\n\n<li>Idle, prepare\uff1a\u7528\u4e8e\u7cfb\u7edf\u5185\u90e8\u51c6\u5907\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>Poll\uff1a\u7b49\u5f85I\/O\u4e8b\u4ef6\uff0c\u6267\u884c\u5230\u671f\u7684\u56de\u8c03\u3002<\/li>\n\n\n\n<li>Check\uff1a\u6267\u884csetImmediate\u6ce8\u518c\u7684\u56de\u8c03\u3002<\/li>\n\n\n\n<li>Close callbacks\uff1a\u5904\u7406close\u4e8b\u4ef6\u7684\u56de\u8c03\uff08\u5982socket.on(&#8216;close&#8217;)\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Promise \u7684\u7f16\u7a0b\u6a21\u578b\u4f9d\u7136\u5145\u65a5\u7740\u5927\u91cf\u7684&nbsp;<code>then<\/code>&nbsp;\u65b9\u6cd5\uff0c\u867d\u7136\u89e3\u51b3\u4e86<code>\u56de\u8c03\u5730\u72f1<\/code>\u7684\u95ee\u9898\uff0c\u4f46\u662f\u5728\u8bed\u4e49\u65b9\u9762\u4f9d\u7136\u5b58\u5728\u7f3a\u9677\uff0c\u4ee3\u7801\u4e2d\u5145\u65a5\u7740\u5927\u91cf\u7684&nbsp;<code>then<\/code>&nbsp;\u51fd\u6570\uff0c\u8fd9\u5c31\u662f async\/await \u51fa\u73b0\u7684\u539f\u56e0\u3002async\/await \u8ba9\u4ee3\u7801\u66f4\u5c11\uff0c\u66f4\u7b80\u6d01\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u5173\u4e8eansync<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5148\u6765\u770b\u770b async \u5230\u5e95\u662f\u4ec0\u4e48\uff1f\u6839\u636e MDN \u5b9a\u4e49\uff0casync \u662f\u4e00\u4e2a\u901a\u8fc7\u5f02\u6b65\u6267\u884c\u5e76\u9690\u5f0f\u8fd4\u56de Promise \u4f5c\u4e3a\u7ed3\u679c\u7684\u51fd\u6570\u3002\u5bf9 async \u51fd\u6570\u7684\u7406\u89e3\uff0c\u8fd9\u91cc\u9700\u8981\u91cd\u70b9\u5173\u6ce8\u4e24\u4e2a\u8bcd\uff1a\u5f02\u6b65\u6267\u884c\u548c\u9690\u5f0f\u8fd4\u56de Promise\u3002 \u4e0b\u9762\u6211\u4eec\u4f1a\u901a\u8fc7\u4f8b\u5b50\u6765\u770b\u5982\u4f55 \u9690\u5f0f\u8fd4\u56de Promise\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">async\u7684\u7528\u6cd5\uff0c\u8bed\u6cd5\u5f88\u7b80\u5355\uff0c\u5728\u51fd\u6570\u524d\u9762\u52a0\u4e0aasync\u5173\u952e\u5b57\uff0c\u8868\u793a\u51fd\u6570\u662f\u5f02\u6b65\u7684\u3002<br><\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code> async function timeout() {\n     return 'hello world!'\n }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">async \u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2apromise \u5bf9\u8c61\uff0c\u5e76\u4e14Promise\u8fd8\u6709state\u548cresult\uff0c\u5982\u679casync\u51fd\u6570\u4e2d\u6709\u8fd4\u56de\u503c\uff0c\u5f53\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u5185\u90e8\u4f1a\u8c03\u7528Promise.resolve()\u65b9\u6cd5\u628a\u5b83\u8f6c\u5316\u6210\u4e00\u4e2apromise\u5bf9\u8c61\u4f5c\u4e3a\u8fd4\u56de\uff0c\u4f46\u5982\u679ctimeout\u51fd\u6570\u5185\u90e8\u629b\u51fa\u9519\u8bef\u5462\uff1f \u90a3\u4e48\u5c31\u4f1a\u8c03\u7528Promise.reject() \u8fd4\u56de\u4e00\u4e2apromise \u5bf9\u8c61<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"515\" height=\"116\" src=\"https:\/\/ikesallows.top\/wp-content\/uploads\/2025\/01\/image.png\" alt=\"\" class=\"wp-image-386\" style=\"width:840px;height:auto\" srcset=\"https:\/\/ikesallows.top\/wp-content\/uploads\/2025\/01\/image.png 515w, https:\/\/ikesallows.top\/wp-content\/uploads\/2025\/01\/image-300x68.png 300w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\u90a3\u4e48\u8981\u60f3\u83b7\u53d6\u5230async \u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\uff0c\u5c31\u8981\u8c03\u7528promise\u7684then \u6216 catch \u6765\u7ed9\u5b83\u6ce8\u518c\u56de\u8c03\u51fd\u6570\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>  async function timeout() {\n      return 'hello world!'\n    }\n    timeout().then(val =&gt; {\n      console.log(val)\n    })\n    console.log('\u6211\u867d\u7136\u5728\u540e\u9762\uff0c\u4f46\u662f\u5148\u6267\u884c')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u83b7\u53d6\u5230\u4e86&#8221;hello world\uff01&#8217;, \u540c\u65f6timeout\u7684\u6267\u884c\u4e5f\u6ca1\u6709\u963b\u585e\u540e\u9762\u4ee3\u7801\u7684\u6267\u884c\uff0c\u548c\u6211\u4eec\u521a\u624d\u8bf4\u7684\u4e00\u81f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>   async function timeout () {\n      for (let index = 0; index &lt; 3; index++) {\n        console.log('async', +index)\n      }\n    }\n    console.log(timeout())\n    console.log('outer')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679casync \u51fd\u6570\u6267\u884c\u5b8c\uff0c\u8fd4\u56de\u7684promise \u6ca1\u6709\u6ce8\u518c\u56de\u8c03\u51fd\u6570\uff0c\u6bd4\u5982\u51fd\u6570\u5185\u90e8\u505a\u4e86\u4e00\u6b21for \u5faa\u73af\uff0c\u4f60\u4f1a\u53d1\u73b0\u51fd\u6570\u7684\u8c03\u7528\uff0c\u5c31\u662f\u6267\u884c\u4e86\u51fd\u6570\u4f53\uff0c\u548c\u666e\u901a\u51fd\u6570\u6ca1\u6709\u533a\u522b\uff0c\u552f\u4e00\u7684\u533a\u522b\u5c31\u662f\u51fd\u6570\u6267\u884c\u5b8c\u4f1a\u8fd4\u56de\u4e00\u4e2a promise \u5bf9\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5916\uff0c<code>async<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a<code>promise<\/code>\u5bf9\u8c61\uff0c\u4e0b\u9762\u4e24\u79cd\u65b9\u6cd5\u662f\u7b49\u6548\u7684<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>\/\/ \u65b9\u6cd51\nfunction f() {return Promise.resolve('TEST');\n}\/\/ asyncF is equivalent to f!\n\n\/\/ \u65b9\u6cd52\nasync function asyncF() {return 'TEST';\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">async \u5173\u952e\u5b57\u6700\u91cd\u8981\u7684\u5c31\u662fasync\u51fd\u6570\u7684\u6267\u884c\u4f1a\u8fd4\u56depromise\u5bf9\u8c61\uff0c\u5e76\u4e14\u628a\u5185\u90e8\u7684\u503c\u8fdb\u884cpromise\u7684\u5c01\u88c5\u3002\u5982\u679cpromise\u5bf9\u8c61\u901a\u8fc7then\u6216catch\u65b9\u6cd5\u53c8\u6ce8\u518c\u4e86<strong>\u56de\u8c03\u51fd\u6570<\/strong>\uff0casync\u51fd\u6570\u6267\u884c\u5b8c\u4ee5\u540e\uff0c<strong>\u6ce8\u518c\u7684\u56de\u8c03\u51fd\u6570\u5c31\u4f1a\u653e\u5230\u5f02\u6b65\u961f\u5217\u4e2d<\/strong>\uff0c\u7b49\u5f85\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u5173\u4e8eawait<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">await\u5173\u952e\u5b57\u53ea\u80fd\u653e\u5230async\u51fd\u6570\u91cc\u9762\uff0cawait\u662f\u7b49\u5f85\u7684\u610f\u601d\uff0c\u90a3\u4e48\u5b83\u7b49\u5f85\u4ec0\u4e48\u5462\uff1f\u5b83\u540e\u9762\u8ddf\u7740\u4ec0\u4e48\u5462\uff1f\u5176\u5b9eawait\u4e0d\u4ec5\u4ec5\u7528\u4e8e\u7b49Promise\u5bf9\u8c61\uff0c\u8fd8\u53ef\u4ee5\u7b49\u4efb\u610f\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5await\u540e\u9762\u5b9e\u9645\u662f\u53ef\u4ee5\u63a5\u666e\u901a\u51fd\u6570\u8c03\u7528\u6216\u8005\u76f4\u63a5\u91cf\u7684\uff0c\u4e0d\u8fc7\u6211\u4eec\u66f4\u591a\u7684\u662f\u653e\u4e00\u4e2a\u8fd4\u56depromise \u5bf9\u8c61\u7684\u8868\u8fbe\u5f0f\u3002\u4ed6\u7b49\u5f85\u7684\u662fpromise\u5bf9\u8c61\u6267\u884c\u5b8c\u6bd5\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>\/\/\u6240\u4ee5\u4e0b\u9762\u8fd9\u4e2a\u793a\u4f8b\u5b8c\u5168\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\n    function getSomething () {\n      return 'something'\n    }\n    async function testAsync () {\n      return Promise.resolve('hello async')\n    }\n    async function test () {\n      const v1 = await getSomething()\n      const v2 = await testAsync()\n      console.log(v1, v2)\n    }\n    test()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u5b83\u7b49\u5230\u7684\u662f\u4e00\u4e2aPromise\u5bf9\u8c61\uff0cawait\u5c31\u5fd9\u8d77\u6765\u4e86\uff0c\u5b83\u4f1a\u963b\u585e\u51fd\u6570\u540e\u9762\u7684\u4ee3\u7801\uff0c\u7b49\u7740Promise\u5bf9\u8c61resolve\/reject\uff0c\u7136\u540e\u5f97\u5230resolve\/reject\u7684\u503c\uff0c\u4f5c\u4e3aawait\u8868\u8fbe\u5f0f\u7684\u8fd0\u7b97\u7ed3\u679c\u3002<br><\/li>\n\n\n\n<li>\u5982\u679c await \u7b49\u5f85\u7684\u662f\u4e00\u4e2a\u975e Promise \u5bf9\u8c61\uff0c\u90a3\u4e48V8 \u4f1a\u9690\u5f0f\u5730\u5c06\u8be5\u5bf9\u8c61\u5305\u88c5\u6210\u4e00\u4e2a\u5df2\u7ecf resolve \u7684 Promise \u5bf9\u8c61.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"> async\/await \u5e2e\u6211\u4eec\u5e72\u4e86\u5565?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u505a\u4e2a\u7b80\u5355\u7684\u6bd4\u8f83<br>\u73b0\u5728\u4e3e\u4f8b\uff0c\u7528 setTimeout \u6a21\u62df\u8017\u65f6\u7684\u5f02\u6b65\u64cd\u4f5c\uff0c\u5148\u6765\u770b\u770b\u4e0d\u7528 async\/await \u4f1a\u600e\u4e48\u5199<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>function takeLongTime () {\n      return new Promise(resolve =&gt; {\n        setTimeout(() =&gt;\n          resolve('long_time_value'), 1000\n        )\n      })\n    }\ntakeLongTime().then(val =&gt; {\n   console.log(val, 'val')\n})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u6539\u7528 async\/await \u5462\uff0c\u4f1a\u662f\u8fd9\u6837<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>    function takeLongTime () {\n      return new Promise(resolve =&gt; {\n        setTimeout(() =&gt;\n          resolve('long_time_value'), 1000\n        )\n      })\n    }\n     async function test () {\n      let v = await takeLongTime()\n      console.log(v, 'v')\n    }\n    test()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u773c\u5c16\u7684\u5df2\u7ecf\u53d1\u73b0&nbsp;<code>takeLongTime ()&nbsp;<\/code>\u6ca1\u6709\u7533\u660e\u4e3a<code>async<\/code>\u3002\u5b9e\u9645\u4e0a<code>takeLongTime ()&nbsp;<\/code>\u672c\u8eab\u5c31\u8fd4\u56dePromise\u5bf9\u8c61\uff0c\u52a0\u4e0d\u52a0async\u7ed3\u679c\u90fd\u4e00\u6837\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>await \u4f18\u52bf\u5728\u4e8e\u5904\u7406 then \u94fe\uff0c\u4f7f\u4ee3\u7801\u770b\u8d77\u6765\u50cf\u540c\u6b65\u4ee3\u7801\u4e00\u6837<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u8ba9\u5b83\u8fd4\u56depromise \u5bf9\u8c61\uff0c\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u662f2s \u4e4b\u540e\u8ba9\u6570\u503c\u4e58\u4ee52<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>\/\/ 2s \u4e4b\u540e\u8fd4\u56de\u53cc\u500d\u7684\u503c\nfunction doubleAfter2seconds (num) {\n      return new Promise((resolve, reject) =&gt; {\n        setTimeout(() =&gt; {\n          resolve(num * 2)\n        }, 2000)\n      })\n    }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u5199\u4e00\u4e2aasync \u51fd\u6570\uff0c\u4ece\u800c\u53ef\u4ee5\u4f7f\u7528await \u5173\u952e\u5b57\uff0c await \u540e\u9762\u653e\u7f6e\u7684\u5c31\u662f\u8fd4\u56depromise\u5bf9\u8c61\u7684\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u5b83\u540e\u9762\u53ef\u4ee5\u5199\u4e0a doubleAfter2seconds \u51fd\u6570\u7684\u8c03\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>async function testResult() {\n    let result = await doubleAfter2seconds(30);\n    console.log(result); \/\/2s\u540e\u6253\u537060\n}\ntestResult();\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b<br>\u8c03\u7528testResult \u51fd\u6570\uff0c\u5b83\u91cc\u9762\u9047\u5230\u4e86await, await \u8868\u793a\u7b49\u5f85\uff0c<strong>\u4ee3\u7801\u5c31\u6682\u505c\u5230\u8fd9\u91cc\uff0c\u4e0d\u518d\u5411\u4e0b\u6267\u884c\u4e86<\/strong>\uff0c\u5b83\u7b49\u5f85\u540e\u9762\u7684promise\u5bf9\u8c61\u6267\u884c\u5b8c\u6bd5\uff0c\u7136\u540e\u62ff\u5230promise resolve \u7684\u503c\u5e76\u8fdb\u884c\u8fd4\u56de\uff0c\u8fd4\u56de\u503c\u62ff\u5230\u4e4b\u540e\uff0c\u5b83\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002\u5177\u4f53\u5230 \u6211\u4eec\u7684\u4ee3\u7801, \u9047\u5230await \u4e4b\u540e\uff0c\u4ee3\u7801\u5c31\u6682\u505c\u6267\u884c\u4e86\uff0c \u7b49\u5f85doubleAfter2seconds(30) \u6267\u884c\u5b8c\u6bd5\uff0cdoubleAfter2seconds(30) \u8fd4\u56de\u7684promise \u5f00\u59cb\u6267\u884c\uff0c2\u79d2 \u4e4b\u540e\uff0cpromise resolve \u4e86\uff0c \u5e76\u8fd4\u56de\u4e86\u503c\u4e3a60\uff0c \u8fd9\u65f6await \u624d\u62ff\u5230\u8fd4\u56de\u503c60\uff0c \u7136\u540e\u8d4b\u503c\u7ed9result\uff0c \u6682\u505c\u7ed3\u675f\uff0c\u4ee3\u7801\u7ee7\u7eed\u6267\u884c\uff0c\u6267\u884c console.log\u8bed\u53e5\u3002<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c31\u8fd9\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u80fd\u770b\u4e0d\u51faasync\/await \u7684\u4f5c\u7528\uff0c\u5982\u679c\u6211\u4eec\u8981\u8ba1\u7b973\u4e2a\u6570\u7684\u503c\uff0c\u7136\u540e\u628a\u5f97\u5230\u7684\u503c\u8fdb\u884c\u8f93\u51fa\u5462\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>async function testResult() {\n    let first = await doubleAfter2seconds(30);\n    let second = await doubleAfter2seconds(50);\n    let third = await doubleAfter2seconds(30);\n    console.log(first + second + third);\n}\ntestResult()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>6\u79d2\u540e\uff0c\u63a7\u5236\u53f0\u8f93\u51fa220<\/strong>, \u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5199\u5f02\u6b65\u4ee3\u7801\u5c31\u50cf\u5199\u540c\u6b65\u4ee3\u7801\u4e00\u6837\u4e86\uff0c\u518d\u4e5f\u6ca1\u6709\u56de\u8c03\u5730\u72f1\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u5f3a\u8c03\u4e00\u4e0b\uff0c\u5f53js\u5f15\u64ce\u5728\u7b49\u5f85promise.resolve\u7684\u65f6\u5019\uff0c\u4ed6\u5e76\u6ca1\u6709\u771f\u6b63\u7684\u6682\u505c\u5de5\u4f5c\uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u5176\u4ed6\u7684\u4e00\u4e9b\u4e8b\u60c5\uff0c\u5982\u679c\u6211\u4eec\u5728testResult\u51fd\u6570\u540e\u9762\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4ee3\u7801\uff0c\u6bd4\u5982console.log\u4e00\u4e0b\uff0c\u4f1a\u53d1\u73b0console.log\u4ee3\u7801\u5148\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background\"><code>async function testResult() {\n    let first = await doubleAfter2seconds(30);\n    let second = await doubleAfter2seconds(50);\n    let third = await doubleAfter2seconds(30);\n    console.log(first + second + third);\n}\ntestResult()\nconsole.log('\u6211\u5148\u6267\u884c\uff01\uff01\uff01')\n\n\u5148\u8f93\u51fa \u201c\u6211\u5148\u6267\u884c\uff01\uff01\uff01\u201d\uff0c6s\u540e\u8f93\u51fa\u8ba1\u7b97\u7ed3\u679c<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"60\" src=\"https:\/\/ikesallows.top\/wp-content\/uploads\/2025\/01\/image-1.png\" alt=\"\" class=\"wp-image-403\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e94\u7528\u4e3e\u4f8b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1\uff09\u5f53\u9047\u5230&nbsp;<code>await<\/code>&nbsp;\u65f6\uff0c\u4f1a\u963b\u585e\u51fd\u6570\u5185\u90e8\u5904\u4e8e\u5b83\u540e\u9762\u7684\u4ee3\u7801\uff08\u800c\u975e\u6574\u6bb5\u4ee3\u7801\uff09\uff0c\u53bb\u6267\u884c\u8be5\u51fd\u6570\u5916\u90e8\u7684\u540c\u6b65\u4ee3\u7801\uff1b\u5f53\u5916\u90e8\u7684\u540c\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\u6bd5\uff0c\u518d\u56de\u5230\u8be5\u51fd\u6570\u6267\u884c\u5269\u4f59\u7684\u4ee3\u7801\u3002\u5e76\u4e14\u5f53&nbsp;<code>await<\/code>&nbsp;\u6267\u884c\u5b8c\u6bd5\u4e4b\u540e\uff0c\u4f1a\u4f18\u5148\u5904\u7406\u5fae\u4efb\u52a1\u961f\u5217\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-1\"><code>async function fn1 (){\n  console.log(1)\n  await fn2() \/\/ \u9047\u5230fn2\uff0c\u8fdb\u5165\u6267\u884c\n  console.log(2) \/\/ \u963b\u585e\uff0c\u52a0\u5165\u5fae\u4efb\u52a1\u961f\u5217\n}\nasync function fn2 (){\n  console.log('fn2')\n}\nfn1()\nconsole.log(3)\n\n\/\/ \u8f93\u51fa\u7ed3\u679c\uff1a1\uff0cfn2\uff0c3\uff0c2\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>await<\/code>&nbsp;\u4f1a\u963b\u585e\u5b83\u4e0b\u9762\u7684\u4ee3\u7801\uff08\u5373\u52a0\u5165<code>\u5fae\u4efb\u52a1\u961f\u5217<\/code>\uff09\uff0c\u5148\u6267\u884c async \u5916\u9762\u7684\u540c\u6b65\u4ee3\u7801\uff0c\u540c\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\uff0c\u518d\u56de\u5230 async \u51fd\u6570\u4e2d\uff0c\u518d\u6267\u884c\u4e4b\u524d\u963b\u585e\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-2\"><code>  async function async1() {\n    console.log('async1 start')\n    await async2()\n    console.log('async1 end')\n    setTimeout(() =&gt; {\n      console.log('timer1')\n    }, 0)\n  }\n  async function async2() {\n    setTimeout(() =&gt; {\n      console.log('timer2')\n    }, 0)\n    console.log('async2')\n  }\n  async1()\n  setTimeout(() =&gt; {\n    console.log('timer3')\n  }, 0)\n  console.log('start')\n\/\/ async1 start =&gt; async2 =&gt; start =&gt; async1 end =&gt; timer2 =&gt; timer3 =&gt; timer1\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9996\u5148\u8fdb\u5165async1\uff0c\u6253\u5370\u51fa async1 start\u3002<\/li>\n\n\n\n<li>\u7136\u540e\u9047\u5230async2\uff0c\u8fdb\u5165async2\uff0c\u9047\u5230\u5b9a\u65f6\u5668 timer2\uff0c\u52a0\u5165\u5b8f\u4efb\u52a1\u961f\u5217\uff0c\u4e4b\u540e\u6253\u5370 async2\u3002<\/li>\n\n\n\n<li>\u7531\u4e8e async2\u963b\u585e\u4e86\u540e\u9762\u7684\u4ee3\u7801\u6267\u884c\uff0c\u5c06\u540e\u9762\u7684\u4ee3\u7801\u52a0\u5165\u5fae\u4efb\u52a1\u961f\u5217\u3002\u6240\u4ee5\u6267\u884c\u540e\u9762\u7684\u5b9a\u65f6\u5668 timer3\uff0c\u5c06\u5176\u52a0\u5165\u5b8f\u4efb\u52a1\u961f\u5217\uff0c\u4e4b\u540e\u6253\u5370\u540c\u6b65\u4ee3\u7801 start\u3002<\/li>\n\n\n\n<li>\u540c\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\u6bd5\uff0c\u5148\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217\uff0c\u6253\u5370\u51fa async1 end\uff0c\u9047\u5230\u5b9a\u65f6\u5668 timer1\uff0c\u5c06\u5176\u52a0\u5165\u5b8f\u4efb\u52a1\u961f\u5217\u3002<\/li>\n\n\n\n<li>\u6700\u540e\u518d\u6267\u884c\u5b8f\u4efb\u52a1\u961f\u5217\uff0c\u5b8f\u4efb\u52a1\u961f\u5217\u67093\u4e2a\u4efb\u52a1\uff0c\u5148\u540e\u987a\u5e8f\u662f timer2\uff0ctimer3\uff0ctimer1\uff0c\u5206\u522b\u6309\u987a\u5e8f\u6267\u884c\uff0c\u4efb\u52a1\u961f\u5217\u6309\u7167\u5148\u8fdb\u5148\u51fa\u539f\u5219\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-3\"><code>async function test() {\n  console.log(100)\n  let x = await 200\n  console.log(x)\n  console.log(200)\n}\nconsole.log(0)\ntest()\nconsole.log(300)\n\/\/0 =&gt; 100 =&gt; 300 =&gt; 200 =&gt; 200\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9996\u5148\u4ee3\u7801\u540c\u6b65\u6267\u884c\uff0c\u6253\u5370\u51fa0\uff1b<\/li>\n\n\n\n<li>\u7136\u540e\u5c06 test() \u538b\u5165\u6267\u884c\u6808\uff0c\u6253\u5370\u51fa100, \u4e0b\u9762\u6ce8\u610f\u4e86\uff0c\u9047\u5230\u4e86\u5173\u952e\u89d2\u8272<code>await<\/code>\uff1b<\/li>\n\n\n\n<li>await 200\uff0c\u88ab JS \u5f15\u64ce\u8f6c\u6362\u6210\u4e00\u4e2a&nbsp;<code>Promise<\/code>&nbsp;:<\/li>\n<\/ul>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background wp-block-paragraph\">let promise = new Promise((resolve,reject) =&gt; {<br>resolve(200);<br>})<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u8c03\u7528\u4e86 resolve\uff0cresolve\u7684\u4efb\u52a1\u8fdb\u5165<code>\u5fae\u4efb\u52a1\u961f\u5217<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7136\u540e\uff0cJS \u5f15\u64ce\u5c06\u6682\u505c\u5f53\u524d\u534f\u7a0b\u7684\u8fd0\u884c\uff0c\u628a\u7ebf\u7a0b\u7684\u6267\u884c\u6743\u4ea4\u7ed9\u7236\u534f\u7a0b\uff1b<\/li>\n\n\n\n<li>\u56de\u5230\u7236\u534f\u7a0b\u4e2d\uff0c\u7236\u534f\u7a0b\u7684\u7b2c\u4e00\u4ef6\u4e8b\u60c5\u5c31\u662f\u5bf9<code>await<\/code>\u8fd4\u56de\u7684<code>Promise<\/code>\u8c03\u7528<code>then<\/code>, \u6765\u76d1\u542c\u8fd9\u4e2a Promise \u7684\u72b6\u6001\u6539\u53d8\uff1b<\/li>\n<\/ul>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background wp-block-paragraph\">promise.then(value =&gt; { <em>\/\/ \u76f8\u5173\u903b\u8f91\uff0c\u5728resolve \u6267\u884c\u4e4b\u540e\u6765\u8c03\u7528<\/em> })<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7136\u540e\u5f80\u4e0b\u6267\u884c\uff0c\u6253\u5370\u51fa300\u3002<\/li>\n\n\n\n<li>\u6839\u636eEventLoop\u673a\u5236\uff0c\u5f53\u524d\u4e3b\u7ebf\u7a0b\u7684\u5b8f\u4efb\u52a1\u5b8c\u6210\uff0c\u73b0\u5728\u68c0\u67e5\u5fae\u4efb\u52a1\u961f\u5217, \u53d1\u73b0\u8fd8\u6709\u4e00\u4e2aPromise\u7684resolve\uff0c\u6267\u884c\uff0c\u73b0\u5728\u7236\u534f\u7a0b\u5728then\u4e2d\u4f20\u5165\u7684\u56de\u8c03\u6267\u884c\u3002\u6211\u4eec\u6765\u770b\u770b\u8fd9\u4e2a\u56de\u8c03\u5177\u4f53\u505a\u7684\u662f\u4ec0\u4e48\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background wp-block-paragraph\">promise.then(value =&gt; { <em>\/\/ 1. \u5c06\u7ebf\u7a0b\u7684\u6267\u884c\u6743\u4ea4\u7ed9test\u534f\u7a0b<\/em> <em>\/\/ 2. \u628a value \u503c\u4f20\u9012\u7ed9 test \u534f\u7a0b<\/em> })<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u73b0\u5728\u6267\u884c\u6743\u5230\u4e86test\u534f\u7a0b\u624b\u4e0a\uff0ctest \u63a5\u6536\u5230\u7236\u534f\u7a0b\u4f20\u6765\u7684200, \u8d4b\u503c\u7ed9 a ,\u7136\u540e\u4f9d\u6b21\u6267\u884c\u540e\u9762\u7684\u8bed\u53e5\uff0c\u6253\u5370200\u3001200\u3002<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<p class=\"wp-block-paragraph\">\u603b\u7684\u6765\u8bf4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Promise \u7684\u7f16\u7a0b\u6a21\u578b\u4f9d\u7136\u5145\u65a5\u7740\u5927\u91cf\u7684 then \u65b9\u6cd5\uff0c\u867d\u7136\u89e3\u51b3\u4e86\u56de\u8c03\u5730\u72f1\u7684\u95ee\u9898\uff0c\u4f46\u662f\u5728\u8bed\u4e49\u65b9\u9762\u4f9d\u7136\u5b58\u5728\u7f3a\u9677\uff0c\u4ee3\u7801\u4e2d\u5145\u65a5\u7740\u5927\u91cf\u7684 then \u51fd\u6570\uff0c\u8fd9\u5c31\u662f async\/await \u51fa\u73b0\u7684\u539f\u56e0\u3002<br>\u4f7f\u7528 async\/await \u53ef\u4ee5\u5b9e\u73b0\u7528\u540c\u6b65\u4ee3\u7801\u7684\u98ce\u683c\u6765\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\uff0c\u8fd9\u662f\u56e0\u4e3a async\/await \u7684\u57fa\u7840\u6280\u672f\u4f7f\u7528\u4e86\u751f\u6210\u5668\u548c Promise\uff0c\u751f\u6210\u5668\u662f\u534f\u7a0b\u7684\u5b9e\u73b0\uff0c\u5229\u7528\u751f\u6210\u5668\u80fd\u5b9e\u73b0\u751f\u6210\u5668\u51fd\u6570\u7684\u6682\u505c\u548c\u6062\u590d\u3002<br>async \u51fd\u6570<br>1)async \u662f\u4e00\u4e2a\u901a\u8fc7\u5f02\u6b65\u6267\u884c\u5e76\u9690\u5f0f\u8fd4\u56de Promise \u4f5c\u4e3a\u7ed3\u679c\u7684\u51fd\u6570\u3002<br>2)Promise\u5bf9\u8c61\u7684\u7ed3\u679c\u7531async\u51fd\u6570\u6267\u884c\u7684\u8fd4\u56de\u503c\u51b3\u5b9a<br>await \u8868\u8fbe\u5f0f<br>1)\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0cawait\u53f3\u4fa7\u7684\u8868\u8fbe\u5f0f\u4e00\u822c\u4e3a promise\u5bf9\u8c61 , \u4f46\u4e5f\u53ef\u4ee5\u662f\u5176\u5b83\u7684\u503c<br>2)\u5982\u679c\u8868\u8fbe\u5f0f\u662fpromise\u5bf9\u8c61\uff0cawait\u5c31\u5fd9\u8d77\u6765\u4e86\uff0c\u5b83\u4f1a\u963b\u585e\u51fd\u6570\u540e\u9762\u7684\u4ee3\u7801\uff0c\u7b49\u7740Promise\u5bf9\u8c61resolve\/reject\uff0c\u7136\u540e\u5f97\u5230resolve\/reject\u7684\u503c\uff0c\u4f5c\u4e3aawait\u8868\u8fbe\u5f0f\u7684\u8fd0\u7b97\u7ed3\u679c\u3002<br>3)\u5982\u679c await \u7b49\u5f85\u7684\u662f\u4e00\u4e2a\u975e Promise \u5bf9\u8c61\uff0c\u90a3\u4e48V8 \u4f1a\u9690\u5f0f\u5730\u5c06\u8be5\u5bf9\u8c61\u5305\u88c5\u6210\u4e00\u4e2a\u5df2\u7ecfresolve \u7684 Promise \u5bf9\u8c61\u3002\u6bd4\u5982 \u4e0a\u9762\u6848\u4f8b await 200\uff0c\u4f1a\u9ed8\u8ba4\u521b\u5efa\u4e00\u4e2a Promise \u5bf9\u8c61\u3002<br>\u5728\u4f7f\u7528await\u7684\u65f6\u5019\u6211\u4eec\u53ea\u662f\u6682\u505c\u4e86\u51fd\u6570\uff0c\u800c\u975e\u6574\u6bb5\u4ee3\u7801\u3002\u8fd9\u91cc\u7ecf\u5e38\u4f1a\u662f\u5bb9\u6613\u72af\u9519\u7684\u5730\u65b9\u3002<br>async\u548cawait\u662f\u975e\u963b\u585e\u7684<br>\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528 Promise\uff0c\u4f8b\u5982Promise.all(p1, p2, p3).\uff0c\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0cp1\u3001p2\u3001p3 \u90fd\u662f Promise \u5b9e\u4f8b\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u5c31\u4f1a\u5148\u8c03\u7528 Promise .resolve\u65b9\u6cd5\uff0c\u5c06\u53c2\u6570\u8f6c\u4e3a Promise \u5b9e\u4f8b\uff0c\u518d\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u53ea\u8981 p1\u3001p2\u3001p3 \u4e4b\u4e2d\u6709\u4e00\u4e2a\u88ab rejected\uff0c\u6574\u4e2a\u72b6\u6001\u5c31\u53d8\u6210 rejected\u3002<br>\u521b\u5efa Promise \u65f6\uff0c\u5e76\u4e0d\u4f1a\u751f\u6210\u5fae\u4efb\u52a1\uff0c\u800c\u662f\u9700\u8981\u7b49\u5230 Promise \u5bf9\u8c61\u8c03\u7528 resolve \u6216\u8005 reject \u51fd\u6570\u65f6\uff0c\u624d\u4f1a\u4ea7\u751f\u5fae\u4efb\u52a1\u3002<br>\u4ea7\u751f\u7684\u5fae\u4efb\u52a1\u5e76\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\uff0c\u800c\u662f\u7b49\u5f85\u5f53\u524d\u5b8f\u4efb\u52a1\u5feb\u8981\u6267\u884c\u7ed3\u675f\u65f6\u518d\u6267\u884c\u3002<br>\u6ce8\u610f<br>1)await\u5fc5\u987b\u5199\u5728async\u51fd\u6570\u4e2d, \u4f46async\u51fd\u6570\u4e2d\u53ef\u4ee5\u6ca1\u6709await<br>2)\u5982\u679cawait\u7684promise\u5931\u8d25\u4e86, \u5c31\u4f1a\u629b\u51fa\u5f02\u5e38, \u9700\u8981\u901a\u8fc7try\u2026catch\u6765\u6355\u83b7\u5904\u7406<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">async\/await or promise\u4f7f\u7528\u65f6\u5230\u5e95\u9009\u62e9\u54ea\u4e2a\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u90e8\u5206\u590d\u6742\u60c5\u51b5\u4e0basync\/await \u7684\u786e\u662f\u6700\u4f18\u89e3\uff0c\u4e2a\u4eba\u89c9\u5f97\u4e5f\u4e0d\u662f\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u662f async\/await \u5199\u8d77\u6765\u66f4\u723d\uff0c\u6700\u4e0d\u723d\u7684\u5c31\u662f\u4ed6\u7684\u9519\u8bef\u5904\u7406\uff0ctry\u2026catch\u8fd9\u4e2a\u4ee3\u7801\u770b\u8d77\u6765\u5c31\u5f88\u5947\u602a\uff08\u5f53\u7136\u4e5f\u6709\u5f88\u591a\u4eba\u559c\u6b22\u8fd9\u79cd\u9519\u8bef\u5904\u7406\u65b9\u5f0f\uff09\u3002\u6240\u4ee5\u6211\u4e2a\u4eba\u7684\u4e60\u60ef\uff0c\u5f53\u53ea\u6709\u4e00\u4e2a\u5f02\u6b65\u8bf7\u6c42\uff0c\u4e14\u9700\u8981\u505a\u9519\u8bef\u5904\u7406\u7684\u60c5\u51b5\u4e0b\uff0c\u66f4\u503e\u5411\u4e8e\u4f7f\u7528 promise\u3002\u6bd4\u5982<\/p>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background wp-block-paragraph\">\/\/ promise<br>getInfo()<br>.then(res =&gt; {<br>\/\/do somethings<br>})<br>.catch(err =&gt; {<br>\/\/do somethings<br>})<\/p>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background wp-block-paragraph\">\/\/ async\/await<br>try {<br>const res = await getInfo()<br>\/\/do somethings<br>} catch (error) {<br>\/\/do somethings<br>}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6709\u5d4c\u5957\u8bf7\u6c42\u7684\u60c5\u51b5\u4e0b\uff0c\u80af\u5b9a\u662f async\/await \u66f4\u76f4\u89c2\u7684\u3002<\/p>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background wp-block-paragraph\">\/\/ promise<br>a(() =&gt; {<br>b(() =&gt; {<br>c()<br>})<br>})<\/p>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background wp-block-paragraph\">\/\/ async\/await<br>await a()<br>await b()<br>await c()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u7136\u4ee3\u7801\u5199\u7684\u597d\u4e0e\u4e0d\u597d\u8fd8\u662f\u53d6\u51b3\u4e8e\u5199\u4ee3\u7801\u7684\u4eba\u7684\u3002<br>\u6bd4\u5982\u4e00\u4e2a\u5e38\u89c1\u7684\u4e1a\u52a1\u573a\u666f\uff1a\u6709\u4e24\u4e2a\u5e76\u53d1\u7684\u5f02\u6b65\u8bf7\u6c42\uff0c\u5728\u90fd\u5b8c\u6210\u540edo something\u3002\u4f46\u5f88\u591a\u4eba\u4f1a\u9519\u8bef\u7684\u7528\u4e32\u884c\u7684\u65b9\u5f0f\u5b9e\u73b0\u4e86\u3002<\/p>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background wp-block-paragraph\">\/\/\u9519\u8bef<br>await a()<br>await b()<br>\/\/\u8fd9\u6837\u53d8\u6210\u4e86 a().then(() =&gt; b() )<br>\/\/ a \u597d\u4e86\u624d\u4f1a\u6267\u884c b<br>done()<\/p>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background wp-block-paragraph\">\/\/\u6b63\u786e<br>await Promise.all([a(), b()])<br>done()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u6709\u4e00\u4e2a\u5c0f\u7ec6\u8282async\/await\u6253\u5305\u540e\u7684\u4ee3\u7801\u5176\u5b9e\u4f1a\u6bd4 promise \u590d\u6742\u5f88\u591a\uff0c \u5f53\u7136\u8fd9\u4e2a\u662f\u4e00\u4e2a\u5ffd\u7565\u4e0d\u8ba1\u5f97\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u603b\u7ed3\uff1a<br>\u6211\u8ba4\u4e3a\u5b83\u4eec\u4e24\u4e2a\u4eba\u5e76\u4e0d\u662f or \u7684\u5173\u7cfb\uff0c\u5728\u7279\u5b9a\u7684\u4e1a\u52a1\u573a\u666f\u4e0b\uff0c\u9009\u62e9\u76f8\u5bf9\u800c\u8a00\u4ee3\u7801\u53ef\u8bfb\u6027\u66f4\u597d\u5730\u89e3\u51b3\u65b9\u6848\u3002\u5177\u4f53\u8be5\u600e\u4e48\u9009\u62e9\u8fd8\u662f\u9700\u8981\u5927\u5bb6\u66f4\u5177\u81ea\u5df1\u56e2\u961f\u7684\u98ce\u683c\u6216\u8005\u81ea\u5df1\u7684\u7406\u89e3\u6765\u5224\u65ad\u3002<br><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">\u4e09.\u591a\u7ebf\u7a0b\u4e0e\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u7684\u5f02\u6b65\u7f16\u7a0b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136\u5355\u7ebf\u7a0b\u5f02\u6b65\u6a21\u578b\u975e\u5e38\u9ad8\u6548\uff0c\u4f46\u5728\u5904\u7406CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\uff0c\u5b83\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u9650\u5236\u3002\u6b64\u65f6\uff0c\u591a\u7ebf\u7a0b\u548c\u5206\u5e03\u5f0f\u7cfb\u7edf\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3.1 \u591a\u7ebf\u7a0b\u5f02\u6b65\u7f16\u7a0b<br>\u5728\u591a\u7ebf\u7a0b\u6a21\u578b\u4e2d\uff0c\u4efb\u52a1\u7684\u5e76\u53d1\u5904\u7406\u662f\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u6765\u5b9e\u73b0\u7684\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u72ec\u7acb\u6267\u884c\u4efb\u52a1\uff0c\u7ebf\u7a0b\u4e4b\u95f4\u901a\u8fc7\u5171\u4eab\u5185\u5b58\u6216\u6d88\u606f\u4f20\u9012\u6765\u534f\u8c03\u548c\u540c\u6b65\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f02\u6b65\u7f16\u7a0b\u53ef\u4ee5\u5728\u591a\u7ebf\u7a0b\u4e2d\u4e0e\u4e8b\u4ef6\u9a71\u52a8\u7ed3\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7CompletableFuture\u6765\u8fdb\u884c\u5f02\u6b65\u7f16\u7a0b\uff0c\u800c\u5e95\u5c42\u7684\u4efb\u52a1\u4f1a\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u5e76\u884c\u6267\u884c\u3002\u5f53\u4efb\u52a1\u5b8c\u6210\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u56de\u8c03\u901a\u77e5\u4e3b\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u7ebf\u7a0b\u7f16\u7a0b\u867d\u7136\u80fd\u591f\u63d0\u9ad8CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u5904\u7406\u80fd\u529b\uff0c\u4f46\u4e5f\u5e26\u6765\u4e86\u7ebf\u7a0b\u540c\u6b65\u3001\u8d44\u6e90\u7ade\u4e89\u7b49\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u5728\u591a\u7ebf\u7a0b\u7684\u5f02\u6b65\u7f16\u7a0b\u4e2d\u9700\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u548c\u907f\u514d\u6b7b\u9501\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3.2 \u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u7684\u5f02\u6b65\u7f16\u7a0b<br>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5f02\u6b65\u7f16\u7a0b\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u6027\u80fd\u4f18\u5316\u7684\u624b\u6bb5\uff0c\u5b83\u8fd8\u662f\u5b9e\u73b0\u7cfb\u7edf\u9ad8\u53ef\u7528\u6027\u548c\u9ad8\u5e76\u53d1\u7684\u5fc5\u8981\u6761\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u5f0f\u901a\u5e38\u6d89\u53ca\u5230\u6d88\u606f\u961f\u5217\uff08\u5982RabbitMQ\u3001Kafka\uff09\u3001\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\u3001\u56de\u8c03\u673a\u5236\u4ee5\u53ca\u5206\u5e03\u5f0f\u4e8b\u52a1\u7b49\u3002\u901a\u8fc7\u5f02\u6b65\u6d88\u606f\u4f20\u9012\uff0c\u7cfb\u7edf\u7684\u4e0d\u540c\u6a21\u5757\u53ef\u4ee5\u72ec\u7acb\u6267\u884c\u800c\u4e0d\u9700\u8981\u76f4\u63a5\u540c\u6b65\u7b49\u5f85\uff0c\u4ece\u800c\u5927\u5927\u51cf\u5c11\u4e86\u5404\u4e2a\u6a21\u5757\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\u548c\u54cd\u5e94\u65f6\u95f4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8bf7\u6c42\u8fdb\u5165Web\u670d\u52a1\u65f6\uff0c\u670d\u52a1\u53ef\u80fd\u9700\u8981\u67e5\u8be2\u591a\u4e2a\u6570\u636e\u5e93\uff0c\u8c03\u7528\u5916\u90e8API\uff0c\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u7b49\u3002\u5982\u679c\u8fd9\u4e9b\u64cd\u4f5c\u662f\u540c\u6b65\u6267\u884c\u7684\uff0c\u7528\u6237\u7684\u8bf7\u6c42\u5c31\u4f1a\u88ab\u957f\u65f6\u95f4\u963b\u585e\u3002\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\uff0c\u7cfb\u7edf\u53ef\u4ee5\u5728\u7b49\u5f85\u5916\u90e8\u8d44\u6e90\u65f6\uff0c\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u7528\u6237\u8bf7\u6c42\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u7cfb\u7edf\u7684\u541e\u5410\u91cf\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3.3 \u6301\u4e45\u5316\u4e0e\u5bb9\u9519\u673a\u5236<br>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5f02\u6b65\u7f16\u7a0b\u8fd8\u9700\u8981\u8003\u8651\u6301\u4e45\u5316\u548c\u5bb9\u9519\u95ee\u9898\u3002\u7531\u4e8e\u7f51\u7edc\u4e0d\u7a33\u5b9a\u6216\u670d\u52a1\u6545\u969c\uff0c\u5f02\u6b65\u4efb\u52a1\u53ef\u80fd\u4f1a\u5931\u8d25\u6216\u4e2d\u65ad\u3002\u56e0\u6b64\uff0c\u5f02\u6b65\u7cfb\u7edf\u9700\u8981\u8bbe\u8ba1\u4efb\u52a1\u7684\u91cd\u8bd5\u673a\u5236\u3001\u5931\u8d25\u56de\u8c03\u3001\u8d85\u65f6\u63a7\u5236\u7b49\u673a\u5236\uff0c\u4ee5\u786e\u4fdd\u7cfb\u7edf\u80fd\u591f\u5728\u51fa\u73b0\u95ee\u9898\u65f6\u81ea\u52a8\u6062\u590d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3.4 \u5fae\u670d\u52a1\u4e0e\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784<br>\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u670d\u52a1\u4e4b\u95f4\u7684\u901a\u4fe1\u5f80\u5f80\u91c7\u7528\u5f02\u6b65\u6d88\u606f\u4f20\u9012\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u6d88\u606f\u961f\u5217\u548c\u4e8b\u4ef6\u9a71\u52a8\u67b6\u6784\uff0c\u5fae\u670d\u52a1\u53ef\u4ee5\u89e3\u8026\u5408\uff0c\u907f\u514d\u76f4\u63a5\u7684\u540c\u6b65\u8c03\u7528\u3002\u6bcf\u4e2a\u5fae\u670d\u52a1\u53ea\u9700\u5173\u6ce8\u81ea\u5df1\u7684\u4efb\u52a1\uff0c\u5b8c\u6210\u540e\u53d1\u5e03\u4e8b\u4ef6\u6216\u6d88\u606f\uff0c\u5176\u4ed6\u670d\u52a1\u53ef\u4ee5\u901a\u8fc7\u5f02\u6b65\u7684\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u7cfb\u7edf\u7684\u6269\u5c55\u6027\u548c\u5bb9\u9519\u80fd\u529b\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">\u56db.\u5f02\u6b65\u7f16\u7a0b\u7684\u6311\u6218\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c3d\u7ba1\u5f02\u6b65\u7f16\u7a0b\u53ef\u4ee5\u5e26\u6765\u5de8\u5927\u7684\u6027\u80fd\u63d0\u5347\uff0c\u4f46\u5b83\u4e5f\u5b58\u5728\u4e00\u4e9b\u6311\u6218\uff1a<\/p>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u56de\u8c03\u5730\u72f1\uff1a\u5728\u590d\u6742\u7684\u5f02\u6b65\u903b\u8f91\u4e2d\uff0c\u56de\u8c03\u51fd\u6570\u7684\u5c42\u7ea7\u53ef\u80fd\u53d8\u5f97\u975e\u5e38\u6df1\uff0c\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5904\u7406\uff1a\u5f02\u6b65\u64cd\u4f5c\u4e2d\u9519\u8bef\u7684\u6355\u83b7\u548c\u5904\u7406\u6bd4\u8f83\u590d\u6742\uff0c\u9700\u8981\u4f7f\u7528Promise\u7684catch()\uff0c\u6216\u8005\u5728async\/await\u4e2d\u4f7f\u7528try\/catch\u6765\u6355\u83b7\u5f02\u5e38\u3002<\/li>\n\n\n\n<li>\u8d44\u6e90\u7ba1\u7406\uff1a\u5728\u5f02\u6b65\u73af\u5883\u4e0b\uff0c\u8d44\u6e90\u7684\u7ba1\u7406\u548c\u91ca\u653e\u53d8\u5f97\u66f4\u4e3a\u590d\u6742\u3002\u6bd4\u5982\u6570\u636e\u5e93\u8fde\u63a5\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\u8d44\u6e90\u9700\u8981\u786e\u4fdd\u5728\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u540e\u88ab\u6b63\u786e\u91ca\u653e\u3002<\/li>\n\n\n\n<li><strong>\u8c03\u8bd5\u56f0\u96be<\/strong>\uff1a\u5f02\u6b65\u4ee3\u7801\u7684\u6267\u884c\u987a\u5e8f\u4e0d\u786e\u5b9a\uff0c\u8c03\u8bd5\u8d77\u6765\u53ef\u80fd\u66f4\u52a0\u56f0\u96be\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><br>4.1 \u89e3\u51b3\u65b9\u6848<br>Promise\u4e0easync\/await\uff1a\u53ef\u4ee5\u901a\u8fc7Promise\u548casync\/await\u6765\u907f\u514d\u56de\u8c03\u5730\u72f1\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<br>\u5f02\u6b65\u9519\u8bef\u5904\u7406\uff1a\u901a\u8fc7try\/catch\u6355\u83b7\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u5f02\u6b65\u64cd\u4f5c\u5931\u8d25\u65f6\u80fd\u591f\u6b63\u786e\u5904\u7406\u9519\u8bef\u3002<br>\u4f7f\u7528\u5f02\u6b65\u6846\u67b6\uff1a\u5982Node.js\u4e2d\u7684Express\u3001Python\u4e2d\u7684<code> asyncio<\/code>\u7b49\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5f02\u6b65\u7f16\u7a0b\u652f\u6301\u548c\u5de5\u5177\uff0c\u7b80\u5316\u4e86\u5f02\u6b65\u7f16\u7a0b\u7684\u590d\u6742\u5ea6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br>\u5f02\u6b65\u7f16\u7a0b\u662f\u73b0\u4ee3\u8ba1\u7b97\u4e2d\u5b9e\u73b0\u9ad8\u6548\u5e76\u53d1\u7684\u6838\u5fc3\u6280\u672f\u4e4b\u4e00\u3002\u65e0\u8bba\u662f\u5728\u5355\u7ebf\u7a0b\u7684\u4e8b\u4ef6\u5faa\u73af\u6a21\u578b\u4e2d\uff0c\u8fd8\u662f\u5728\u591a\u7ebf\u7a0b\u548c\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u7684\u5e94\u7528\uff0c\u5f02\u6b65\u7f16\u7a0b\u90fd\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u6269\u5c55\u6027\u3002\u7406\u89e3\u5f02\u6b65\u7f16\u7a0b\u7684\u539f\u7406\uff0c\u638c\u63e1\u4e8b\u4ef6\u5faa\u73af\u3001\u534f\u7a0b\u3001\u6d88\u606f\u961f\u5217\u7b49\u6280\u672f\uff0c\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u8bbe\u8ba1\u51fa\u9ad8\u6548\u3001\u7a33\u5b9a\u7684\u5e76\u53d1\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u5f02\u6b65\u7f16\u7a0b\u4e5f\u4f34\u968f\u7740\u4e00\u4e9b\u6311\u6218\uff0c\u5982\u8c03\u8bd5\u590d\u6742\u6027\u3001\u9519\u8bef\u5904\u7406\u7b49\uff0c\u9700\u8981\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u4e0d\u65ad\u603b\u7ed3\u7ecf\u9a8c\u3001\u4f7f\u7528\u5408\u9002\u7684\u5de5\u5177\u6765\u5e94\u5bf9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53c2\u8003\u539f\u6587\u94fe\u63a51\uff1a<a href=\"https:\/\/blog.csdn.net\/m0_38141444\/article\/details\/144972532\">https:\/\/blog.csdn.net\/m0_38141444\/article\/details\/144972532<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53c2\u8003\u539f\u672c\u94fe\u63a52\uff1a<a href=\"https:\/\/blog.csdn.net\/weixin_45811256\/article\/details\/123638582\">https:\/\/blog.csdn.net\/weixin_45811256\/article\/details\/123638582<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.\u57fa\u672c\u6982\u5ff5 \u5f02\u6b65\u51fd\u6570\u901a\u8fc7\u975e\u963b\u585e\u7684\u65b9\u5f0f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":16,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,43,7],"tags":[45,44],"class_list":["post-360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-42","category-43","category-7","tag-45","tag-44"],"_links":{"self":[{"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":49,"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":416,"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/posts\/360\/revisions\/416"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/media\/16"}],"wp:attachment":[{"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikesallows.top\/index.php\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}