{"id":235184,"date":"2024-05-12T10:15:18","date_gmt":"2024-05-12T02:15:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/235184.html"},"modified":"2024-05-12T10:15:31","modified_gmt":"2024-05-12T02:15:31","slug":"javascript%e5%a6%82%e4%bd%95%e5%90%8c%e6%ad%a5%e6%89%a7%e8%a1%8c%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/235184.html","title":{"rendered":"JavaScript\u5982\u4f55\u540c\u6b65\u6267\u884c\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26234050\/88bd4594-6219-4b29-826c-dcf91614e404.webp\" alt=\"JavaScript\u5982\u4f55\u540c\u6b65\u6267\u884c\u4ee3\u7801\" \/><\/p>\n<p><p>JavaScript\u901a\u5e38\u4ee5\u975e\u963b\u585e\u3001\u5f02\u6b65\u7684\u65b9\u5f0f\u8fd0\u884c\u4ee3\u7801\uff0c\u4f46\u5982\u679c\u4f60\u9700\u8981\u4ee5\u540c\u6b65\u65b9\u5f0f\u6267\u884c\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<strong>\u56de\u8c03\u51fd\u6570\u3001Promises\u7ed3\u5408async\/aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\u3001\u4ee5\u53ca\u4f7f\u7528\u540c\u6b65API<\/strong>\u65b9\u6cd5\u5b9e\u73b0\u3002\u4ee5<strong>Promises\u7ed3\u5408async\/await<\/strong>\u4e3a\u4f8b\uff0c\u8fd9\u79cd\u505a\u6cd5\u5141\u8bb8\u4f60\u4ee5\u8fd1\u4e4e\u540c\u6b65\u7684\u65b9\u5f0f\u7f16\u5199\u4ee3\u7801\uff0c\u540c\u65f6\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u5f02\u6b65\u6267\u884c\u7684\u4f18\u70b9\u3002<\/p>\n<\/p>\n<p><p>Promises\u662f\u4e00\u79cd\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u5b83\u4ee3\u8868\u4e00\u4e2a\u53ef\u80fd\u73b0\u5728\u4e5f\u53ef\u80fd\u5c06\u6765\u5b58\u5728\u7684\u503c\u3002\u901a\u8fc7Promises\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u5f02\u6b65\u6d41\u7a0b\u3002\u800casync\/await\u662f\u5efa\u7acb\u5728Promises\u4e4b\u4e0a\u7684\u8bed\u6cd5\u7cd6\uff0c\u8ba9\u5f02\u6b65\u4ee3\u7801\u9605\u8bfb\u4e0a\u50cf\u540c\u6b65\u4ee3\u7801\u3002\u4e00\u4e2a\u51fd\u6570\u524d\u52a0\u4e0a<code>async<\/code>\u5173\u952e\u5b57\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u8be5\u51fd\u6570\u5185\u4f7f\u7528<code>await<\/code>\u5173\u952e\u5b57\uff0c\u7b49\u5f85\u4e00\u4e2aPromise\u89e3\u51b3(resolve)\u3002<code>await<\/code>\u5c06\u6682\u505c\u4ee3\u7801\u5728\u6b64\u4e4b\u540e\u7684\u6267\u884c\uff0c\u76f4\u5230Promise\u89e3\u51b3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u56de\u8c03\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5728JavaScript\u4e2d\u5b9e\u73b0\u540c\u6b65\u884c\u4e3a\u7684\u4f20\u7edf\u65b9\u5f0f\u3002\u5b83\u662f\u4e00\u4e2a\u5f53\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210\u65f6\u5c31\u4f1a\u88ab\u6267\u884c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u4efb\u52a1\u961f\u5217\u548c\u4e8b\u4ef6\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u56de\u8c03\u51fd\u6570\u4f9d\u8d56\u4e8eJavaScript\u7684\u4e8b\u4ef6\u5faa\u73af\u7cfb\u7edf\u548c\u4efb\u52a1\u961f\u5217\u3002\u5f53\u4f60\u6267\u884c\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u5982<code>setTimeout<\/code>\uff0c\u5b9e\u9645\u7684\u56de\u8c03\u51fd\u6570\u4f1a\u88ab\u653e\u5165\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u53ea\u6709\u5f53\u6267\u884c\u6808\u4e3a\u7a7a\u65f6\uff0c\u4e8b\u4ef6\u5faa\u73af\u624d\u4f1a\u4ece\u4efb\u52a1\u961f\u5217\u4e2d\u53d6\u51fa\u56de\u8c03\u51fd\u6570\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u540c\u6b65\u5f0f\u56de\u8c03<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u56de\u8c03\u51fd\u6570\u7ecf\u5e38\u7528\u4e8e\u5f02\u6b65\u64cd\u4f5c\uff0c\u5b83\u4eec\u4e5f\u53ef\u4ee5\u540c\u6b65\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u7684<code>.forEach()<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u4e14\u662f\u540c\u6b65\u6267\u884c\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Promises\u4e0easync\/await<\/h3>\n<\/p>\n<p><p>Promises\u4e3a\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u53ef\u9760\u548c\u53ef\u7ec4\u7ec7\u7684\u65b9\u5f0f\u3002\u800casync\/await\u662f\u5efa\u7acb\u5728Promises\u4e4b\u4e0a\u7684\uff0c\u5b83\u8ba9\u4f60\u4ee5\u4e00\u79cd\u770b\u4f3c\u540c\u6b65\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>Promise\u7684\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aPromise\u5bf9\u8c61\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u6267\u884c\u5668\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u51fd\u6570\u53c2\u6570\uff1aresolve\u548creject\u3002\u5f53\u5f02\u6b65\u4efb\u52a1\u6210\u529f\u65f6\u8c03\u7528resolve\uff0c\u5931\u8d25\u65f6\u8c03\u7528reject\u3002<\/p>\n<\/p>\n<p><h4>Async\/Await\u7684\u5a01\u529b<\/h4>\n<\/p>\n<p><p>\u7528<code>async<\/code>\u58f0\u660e\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff0c\u8fd9\u4f7f\u5f97\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528<code>await<\/code>\u8868\u8fbe\u5f0f\u3002<code>await<\/code>\u4f1a\u6682\u505c\u51fd\u6570\u7684\u6267\u884c\uff0c\u7b49\u5f85Promise\u89e3\u51b3\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u540c\u6b65API<\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u4e00\u4e9b\u64cd\u4f5c\u63d0\u4f9b\u4e86\u540c\u6b65\u7684API\u3002\u8fd9\u4e9b\u540c\u6b65API\u4f1a\u963b\u585e\u4ee3\u7801\u7684\u8fdb\u4e00\u6b65\u6267\u884c\uff0c\u76f4\u5230\u64cd\u4f5c\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h4>\u540c\u6b65\u65b9\u6cd5\u4f8b\u5b50<\/h4>\n<\/p>\n<p><p>\u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c\u5728Node.js\u4e2d\u6709\u540c\u6b65\u7248\u672c\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>fs.readFileSync()<\/code>\u5c31\u662f\u4e00\u4e2a\u4f1a\u963b\u585e\u8c03\u7528\u7ebf\u7a0b\u76f4\u5230\u6587\u4ef6\u88ab\u8bfb\u53d6\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u540c\u6b65API\u7684\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u540c\u6b65API\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5b83\u4eec\u4f1a\u963b\u585e\u4e8b\u4ef6\u5faa\u73af\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6216\u5bc6\u96c6\u7684\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u751f\u6210\u5668\u548c\u534f\u7a0b<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u5141\u8bb8\u4f60\u7684\u4ee3\u7801\u5728\u6267\u884c\u4e2d\u65ad\u548c\u6062\u590d\u3002\u800c\u534f\u7a0b\u662f\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u522b\u7684\u6982\u5ff5\uff0c\u8868\u73b0\u4e3a\u53ef\u4ee5\u6682\u505c\u6267\u884c\u5e76\u5728\u7a0d\u540e\u67d0\u4e2a\u65f6\u523b\u6062\u590d\u7684\u5b50\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>\u751f\u6210\u5668\u51fd\u6570\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>function*<\/code>\u58f0\u660e\u751f\u6210\u5668\u51fd\u6570\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u8bcd\u6765\u6682\u505c\u51fd\u6570\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u534f\u7a0b\u5e93<\/h4>\n<\/p>\n<p><p>\u4e00\u4e9bJavaScript\u5e93\uff0c\u5982<code>co<\/code>\uff0c\u63d0\u4f9b\u4e86\u534f\u7a0b\u7684\u652f\u6301\uff0c\u4f7f\u5f97\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u66f4\u52a0\u4f18\u96c5\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>JavaScript\u7684\u5f02\u6b65\u7279\u6027\u662f\u5176\u6838\u5fc3\u7279\u6027\u4e4b\u4e00\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u7f16\u5199\u9ad8\u6548\u975e\u963b\u585e\u4ee3\u7801\u7684\u80fd\u529b\u3002\u7136\u800c\uff0c\u5728\u6709\u4e9b\u573a\u666f\u4e0b\uff0c\u540c\u6b65\u884c\u4e3a\u662f\u5fc5\u8981\u7684\u3002\u901a\u8fc7\u4e0a\u9762\u6240\u8ff0\u7684\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u9700\u8981\u7684\u65f6\u5019\u540c\u6b65\u6267\u884cJavaScript\u4ee3\u7801\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fc7\u5206\u4f9d\u8d56\u540c\u6b65\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u7684\u54cd\u5e94\u80fd\u529b\u548c\u6027\u80fd\uff0c\u56e0\u6b64\u5e94\u5f53\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4ec0\u4e48\u662fJavaScript\u540c\u6b65\u6267\u884c\u4ee3\u7801\uff1f<\/strong> <\/p>\n<p>\u540c\u6b65\u6267\u884c\u4ee3\u7801\u662fJavaScript\u4e2d\u7684\u4e00\u79cd\u6267\u884c\u6a21\u5f0f\uff0c\u5b83\u6309\u7167\u4ee3\u7801\u7684\u987a\u5e8f\u5728\u4e00\u4e2a\u4efb\u52a1\u5b8c\u6210\u4e4b\u540e\u518d\u8fdb\u884c\u4e0b\u4e00\u4e2a\u4efb\u52a1\u3002\u76f8\u5bf9\u4e8e\u5f02\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u540c\u6b65\u6267\u884c\u4ee3\u7801\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u4f1a\u6682\u505c\u5176\u4ed6\u4efb\u52a1\u7684\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728JavaScript\u4e2d\u5b9e\u73b0\u540c\u6b65\u6267\u884c\u4ee3\u7801\uff1f<\/strong> <\/p>\n<p>\u5728JavaScript\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u624b\u6bb5\u6765\u5b9e\u73b0\u540c\u6b65\u6267\u884c\u4ee3\u7801\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5ef6\u8fdf\u51fd\u6570\u6216\u5b9a\u65f6\u5668\uff0c\u901a\u8fc7\u8bbe\u7f6e\u9002\u5f53\u7684\u65f6\u95f4\u95f4\u9694\u6765\u63a7\u5236\u4ee3\u7801\u7684\u987a\u5e8f\u6267\u884c\u3002<\/p>\n<p>\u53e6\u4e00\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u548c\u4e8b\u4ef6\u76d1\u542c\uff0c\u901a\u8fc7\u5728\u7279\u5b9a\u7684\u4e8b\u4ef6\u53d1\u751f\u65f6\u89e6\u53d1\u51fd\u6570\u7684\u6267\u884c\uff0c\u4ece\u800c\u5b9e\u73b0\u4ee3\u7801\u7684\u540c\u6b65\u6267\u884c\u3002<\/p>\n<p><strong>\u540c\u6b65\u6267\u884c\u4ee3\u7801\u7684\u4f18\u7f3a\u70b9\u662f\u4ec0\u4e48\uff1f<\/strong> <\/p>\n<p>\u540c\u6b65\u6267\u884c\u4ee3\u7801\u7684\u4f18\u70b9\u662f\u7b80\u6d01\u660e\u4e86\uff0c\u4fbf\u4e8e\u9605\u8bfb\u548c\u8c03\u8bd5\u3002\u7531\u4e8e\u4ee3\u7801\u6309\u7167\u987a\u5e8f\u6267\u884c\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u8ffd\u8e2a\u4ee3\u7801\u7684\u6267\u884c\u6d41\u7a0b\u3002<\/p>\n<p>\u7136\u800c\uff0c\u540c\u6b65\u6267\u884c\u4ee3\u7801\u7684\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\u3002\u5f53\u6267\u884c\u65f6\u95f4\u8f83\u957f\u7684\u4efb\u52a1\u65f6\uff0c\u540c\u6b65\u6267\u884c\u4f1a\u5bfc\u81f4\u9875\u9762\u7684\u963b\u585e\uff0c\u7528\u6237\u4f53\u9a8c\u4e0b\u964d\u3002\u6b64\u5916\uff0c\u5728\u4e00\u4e9b\u9700\u8981\u7b49\u5f85\u5916\u90e8\u8d44\u6e90\u52a0\u8f7d\u5b8c\u6210\u7684\u60c5\u51b5\u4e0b\uff0c\u540c\u6b65\u6267\u884c\u53ef\u80fd\u4f1a\u9020\u6210\u9875\u9762\u7684\u5361\u987f\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u5f02\u6b65\u6267\u884c\u4ee3\u7801\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002\u5f02\u6b65\u6267\u884c\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Promise\u3001async\/await\u7b49\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript\u901a\u5e38\u4ee5\u975e\u963b\u585e\u3001\u5f02\u6b65\u7684\u65b9\u5f0f\u8fd0\u884c\u4ee3\u7801\uff0c\u4f46\u5982\u679c\u4f60\u9700\u8981\u4ee5\u540c\u6b65\u65b9\u5f0f\u6267\u884c\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u3001Pr [&hellip;]","protected":false},"author":3,"featured_media":235196,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/235184"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=235184"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/235184\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/235196"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=235184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=235184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=235184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}