{"id":18608,"date":"2025-07-30T16:18:40","date_gmt":"2025-07-30T08:18:40","guid":{"rendered":"https:\/\/92it.top\/?p=18608"},"modified":"2025-07-30T16:18:40","modified_gmt":"2025-07-30T08:18:40","slug":"nodejs-%e4%bb%a3%e7%a0%81-await-%e4%b8%80%e7%9b%b4%e4%b8%aa%e9%95%bf%e6%97%b6%e9%97%b4%e7%9a%84%e5%8a%a8%e4%bd%9c%ef%bc%8c%e4%bc%9a%e9%98%bb%e5%a1%9e%e4%b8%bb%e7%ba%bf%e7%a8%8b%e5%90%97","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=18608","title":{"rendered":"Nodejs \u4ee3\u7801 await \u4e00\u76f4\u4e2a\u957f\u65f6\u95f4\u7684\u52a8\u4f5c\uff0c\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b\u5417"},"content":{"rendered":"\n<p>\u5728 Node.js \u4e2d\uff0c\u5982\u679c\u4ee3\u7801\u4e2d\u4f7f\u7528\u00a0<code>await<\/code>\u00a0\u7b49\u5f85\u4e00\u4e2a\u957f\u65f6\u95f4\u64cd\u4f5c\uff08\u5982\u8017\u65f6\u7684\u6570\u636e\u5e93\u67e5\u8be2\u3001\u6587\u4ef6\u8bfb\u5199\u3001API \u8c03\u7528\u7b49\uff09\uff0c<strong>\u5f53\u524d\u7684\u4e8b\u4ef6\u5faa\u73af\u4f1a\u88ab\u963b\u585e\u5417\uff1f\u5176\u4ed6\u8bf7\u6c42\u8fd8\u80fd\u88ab\u5904\u7406\u5417\uff1f<\/strong><\/p>\n\n\n\n<p>\u7b54\u6848\u662f\uff1a<strong>\u53d6\u51b3\u4e8e\u8fd9\u4e2a \u201c\u957f\u65f6\u95f4\u64cd\u4f5c\u201d \u7684\u7c7b\u578b<\/strong>\u3002<\/p>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>1. \u5173\u952e\u524d\u63d0\uff1aNode.js \u662f\u5355\u7ebf\u7a0b + \u4e8b\u4ef6\u5faa\u73af\u6a21\u578b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Node.js \u7684\u6838\u5fc3\u7279\u70b9\u662f<strong>\u5355\u7ebf\u7a0b\u6267\u884c JavaScript \u4ee3\u7801<\/strong>\uff0c\u4f46\u901a\u8fc7 \u201c\u4e8b\u4ef6\u5faa\u73af\u201d \u548c \u201c\u975e\u963b\u585e I\/O\u201d \u5b9e\u73b0\u4e86\u9ad8\u5e76\u53d1\u3002<\/p>\n\n\n\n<ul>\n<li>\u5355\u7ebf\u7a0b\u610f\u5473\u7740\uff1a\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6267\u884c\u4e00\u6bb5 JavaScript \u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u4e8b\u4ef6\u5faa\u73af\uff1a\u8d1f\u8d23\u8c03\u5ea6\u5f02\u6b65\u4efb\u52a1\uff08\u5982 I\/O \u64cd\u4f5c\u3001\u5b9a\u65f6\u5668\u7b49\uff09\uff0c\u5f53\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210\u540e\uff0c\u5176\u56de\u8c03\u4f1a\u88ab\u653e\u5165\u961f\u5217\uff0c\u7b49\u5f85\u4e3b\u7ebf\u7a0b\u7a7a\u95f2\u65f6\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>2. \u5206\u4e24\u79cd\u60c5\u51b5\u8ba8\u8bba<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u60c5\u51b5 1\uff1a<code>await<\/code>&nbsp;\u7684\u662f<strong>\u5f02\u6b65\u975e\u963b\u585e\u64cd\u4f5c<\/strong>\uff08\u5982 I\/O \u4efb\u52a1\uff09<\/p>\n\n\n\n<p>\u5982\u679c&nbsp;<code>await<\/code>&nbsp;\u7684\u662f\u4e00\u4e2a<strong>\u5f02\u6b65\u975e\u963b\u585e\u64cd\u4f5c<\/strong>\uff08\u5982\u6570\u636e\u5e93\u67e5\u8be2\u3001\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u7531 Node.js \u7684\u5e95\u5c42\u7ebf\u7a0b\u6c60\u5904\u7406\uff0c\u4e0d\u5360\u7528\u4e3b\u7ebf\u7a0b\uff09\uff0c\u90a3\u4e48\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5f53\u6267\u884c\u5230\u00a0<code>await<\/code>\u00a0\u65f6\uff0c\u5f53\u524d\u7684\u51fd\u6570\u4f1a\u88ab \u201c\u6302\u8d77\u201d\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u91ca\u653e\u51fa\u6765\uff0c\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u8bf7\u6c42\u6216\u4e8b\u4ef6\u5faa\u73af\u4e2d\u7684\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>\u6b64\u65f6\uff0cNode.js \u53ef\u4ee5\u6b63\u5e38\u63a5\u6536\u5e76\u5904\u7406\u5176\u4ed6\u65b0\u7684\u8bf7\u6c42\u3002<\/li>\n\n\n\n<li>\u5f53\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u5176\u7ed3\u679c\u4f1a\u88ab\u8fd4\u56de\uff0c<code>await<\/code>\u00a0\u540e\u7684\u4ee3\u7801\u4f1a\u5728\u4e3b\u7ebf\u7a0b\u7a7a\u95f2\u65f6\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u4f2a\u4ee3\u7801\uff1aawait \u4e00\u4e2a\u5f02\u6b65 I\/O \u64cd\u4f5c\uff08\u975e\u963b\u585e\uff09\napp.get('\/long-task', async (req, res) => {\n  console.log('\u5f00\u59cb\u5904\u7406\u8bf7\u6c42 A');\n  \/\/ \u5047\u8bbe\u8fd9\u662f\u4e00\u4e2a\u8017\u65f6 5 \u79d2\u7684\u6570\u636e\u5e93\u67e5\u8be2\uff08\u5f02\u6b65\u975e\u963b\u585e\uff09\n  await db.query('SELECT * FROM large_table'); \n  res.send('\u8bf7\u6c42 A \u5904\u7406\u5b8c\u6210');\n});\n\napp.get('\/fast-task', (req, res) => {\n  res.send('\u8bf7\u6c42 B \u5904\u7406\u5b8c\u6210');\n});<\/pre>\n\n\n\n<ul>\n<li>\u5f53\u8bf7\u6c42 A \u5230\u8fbe\u5e76\u6267\u884c\u5230\u00a0<code>await db.query(...)<\/code>\u00a0\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u91ca\u653e\uff0c\u6b64\u65f6\u5982\u679c\u6709\u8bf7\u6c42 B \u5230\u8fbe\uff0c\u4f1a\u88ab\u7acb\u5373\u5904\u7406\uff08\u65e0\u9700\u7b49\u5f85 5 \u79d2\uff09\u3002<\/li>\n\n\n\n<li>\u7ed3\u8bba\uff1a<strong>\u53ef\u4ee5\u63a5\u6536\u5e76\u5904\u7406\u5176\u4ed6\u8bf7\u6c42<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>\u60c5\u51b5 2\uff1a<code>await<\/code>&nbsp;\u7684\u662f<strong>\u540c\u6b65\u963b\u585e\u64cd\u4f5c<\/strong>\uff08\u5982\u7eaf CPU \u8ba1\u7b97\uff09<\/p>\n\n\n\n<p>\u5982\u679c&nbsp;<code>await<\/code>&nbsp;\u7684\u662f\u4e00\u4e2a<strong>\u540c\u6b65\u963b\u585e\u64cd\u4f5c<\/strong>\uff08\u5982\u7eaf JavaScript \u5b9e\u73b0\u7684\u590d\u6742\u8ba1\u7b97\u3001\u5faa\u73af\u7b49\uff0c\u5b8c\u5168\u5360\u7528\u4e3b\u7ebf\u7a0b\uff09\uff0c\u90a3\u4e48\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5f53\u6267\u884c\u5230\u00a0<code>await<\/code>\u00a0\u65f6\uff0c\u7531\u4e8e\u64cd\u4f5c\u672c\u8eab\u662f\u540c\u6b65\u7684\uff08\u6ca1\u6709\u4ea4\u7ed9\u5e95\u5c42\u7ebf\u7a0b\u6c60\uff0c\u800c\u662f\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\uff09\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u88ab<strong>\u6301\u7eed\u5360\u7528<\/strong>\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>\u6b64\u65f6\uff0c\u65b0\u7684\u8bf7\u6c42\u4f1a\u88ab\u653e\u5165\u4e8b\u4ef6\u961f\u5217\u4e2d\u6392\u961f\uff0c\u76f4\u5230\u8fd9\u4e2a\u957f\u65f6\u95f4\u64cd\u4f5c\u5b8c\u6210\u3001\u4e3b\u7ebf\u7a0b\u7a7a\u95f2\u540e\u624d\u80fd\u88ab\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u4f2a\u4ee3\u7801\uff1aawait \u4e00\u4e2a\u540c\u6b65\u963b\u585e\u64cd\u4f5c\uff08\u5360\u7528\u4e3b\u7ebf\u7a0b\uff09\napp.get('\/long-calc', async (req, res) => {\n  console.log('\u5f00\u59cb\u5904\u7406\u8bf7\u6c42 A');\n  \/\/ \u5047\u8bbe\u8fd9\u662f\u4e00\u4e2a\u8017\u65f6 5 \u79d2\u7684\u7eaf CPU \u8ba1\u7b97\uff08\u540c\u6b65\u963b\u585e\uff09\n  await new Promise(resolve => {\n    let result = 0;\n    for (let i = 0; i &lt; 10000000000; i++) { \/\/ \u8017\u65f6\u5faa\u73af\n      result += i;\n    }\n    resolve(result);\n  });\n  res.send('\u8bf7\u6c42 A \u5904\u7406\u5b8c\u6210');\n});\n\napp.get('\/fast-task', (req, res) => {\n  res.send('\u8bf7\u6c42 B \u5904\u7406\u5b8c\u6210');\n});<\/pre>\n\n\n\n<ul>\n<li>\u5f53\u8bf7\u6c42 A \u6267\u884c\u5230\u00a0<code>await<\/code>\u00a0\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u88ab\u5faa\u73af\u5360\u7528 5 \u79d2\uff0c\u671f\u95f4\u8bf7\u6c42 B \u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u5230 5 \u79d2\u540e\u624d\u4f1a\u88ab\u5904\u7406\u3002<\/li>\n\n\n\n<li>\u7ed3\u8bba\uff1a<strong>\u65e0\u6cd5\u63a5\u6536\u5e76\u5904\u7406\u5176\u4ed6\u8bf7\u6c42\uff0c\u4f1a\u5bfc\u81f4\u6240\u6709\u8bf7\u6c42\u6392\u961f\u7b49\u5f85<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>3. \u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul>\n<li>\u5982\u679c\u00a0<code>await<\/code>\u00a0\u7684\u662f<strong>\u5f02\u6b65\u975e\u963b\u585e\u64cd\u4f5c<\/strong>\uff08\u5982 I\/O \u4efb\u52a1\uff09\uff1a<strong>\u53ef\u4ee5\u63a5\u6536\u5176\u4ed6\u8bf7\u6c42<\/strong>\uff0c\u56e0\u4e3a\u4e3b\u7ebf\u7a0b\u4f1a\u88ab\u91ca\u653e\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u00a0<code>await<\/code>\u00a0\u7684\u662f<strong>\u540c\u6b65\u963b\u585e\u64cd\u4f5c<\/strong>\uff08\u5982\u7eaf CPU \u8ba1\u7b97\uff09\uff1a<strong>\u65e0\u6cd5\u63a5\u6536\u5176\u4ed6\u8bf7\u6c42<\/strong>\uff0c\u56e0\u4e3a\u4e3b\u7ebf\u7a0b\u88ab\u957f\u65f6\u95f4\u5360\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5728 Node.js \u4e2d\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\u957f\u65f6\u95f4\u7684\u540c\u6b65\u64cd\u4f5c\uff0c\u4ee5\u4fdd\u8bc1\u4e8b\u4ef6\u5faa\u73af\u7684\u987a\u7545\uff0c\u786e\u4fdd\u80fd\u5904\u7406\u5e76\u53d1\u8bf7\u6c42\u3002\u5bf9\u4e8e\u8017\u65f6\u7684 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u8003\u8651\u4f7f\u7528&nbsp;<code>child_process<\/code>&nbsp;\u6216&nbsp;<code>worker_threads<\/code>&nbsp;\u5f00\u542f\u5b50\u8fdb\u7a0b \/ \u7ebf\u7a0b\u5904\u7406\uff0c\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>4. \u5982\u679c\u662f await axios.post \u5462 \u4f1a\u963b\u585e\u5417<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><code>await axios.post<\/code>\u00a0\u7684\u884c\u4e3a\u9700\u8981\u7ed3\u5408\u00a0<strong>Node.js \u7684\u5f02\u6b65\u6a21\u578b<\/strong>\u00a0\u548c\u00a0<strong><code>axios<\/code>\u00a0\u672c\u8eab\u7684\u5b9e\u73b0<\/strong>\u00a0\u6765\u5206\u6790\uff0c\u7ed3\u8bba\u662f\uff1a<strong>\u4e0d\u4f1a\u963b\u585e\u6574\u4e2a\u5e94\u7528\u5904\u7406\u5176\u4ed6\u8bf7\u6c42<\/strong>\uff0c\u4f46\u4f1a \u201c\u6682\u505c\u201d \u5f53\u524d\u5f02\u6b65\u51fd\u6570\u7684\u6267\u884c\uff0c\u76f4\u5230\u00a0<code>axios.post<\/code>\u00a0\u5b8c\u6210\u3002\u4e0b\u9762\u8be6\u7ec6\u62c6\u89e3\uff1a<\/p>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>1. \u6838\u5fc3\u539f\u7406\uff1a<code>axios.post<\/code>&nbsp;\u662f\u5f02\u6b65\u975e\u963b\u585e\u64cd\u4f5c<\/p>\n\n\n\n<ul>\n<li><strong><code>axios<\/code>\u00a0\u7684\u672c\u8d28<\/strong>\uff1a<br><code>axios.post<\/code>\u00a0\u5185\u90e8\u662f\u57fa\u4e8e Node.js \u7684\u00a0<code>http<\/code>\u00a0\u6a21\u5757\uff08\u6216\u6d4f\u89c8\u5668\u7684\u00a0<code>XMLHttpRequest\/fetch<\/code>\uff09\u5b9e\u73b0\u7684\u00a0<strong>\u5f02\u6b65 I\/O \u64cd\u4f5c<\/strong>\uff0c\u4f1a\u4ea4\u7ed9\u5e95\u5c42\u7684\u7ebf\u7a0b\u6c60\uff08Node.js\uff09\u6216\u6d4f\u89c8\u5668\u7684\u4e8b\u4ef6\u5faa\u73af\u5904\u7406\uff0c<strong>\u4e0d\u4f1a\u5360\u7528\u4e3b\u7ebf\u7a0b<\/strong>\u3002<\/li>\n\n\n\n<li><strong><code>await<\/code>\u00a0\u7684\u4f5c\u7528<\/strong>\uff1a<br><code>await<\/code>\u00a0\u4f1a \u201c\u6682\u505c\u201d \u5f53\u524d\u00a0<code>async<\/code>\u00a0\u51fd\u6570\u7684\u6267\u884c\uff0c\u7b49\u5f85\u00a0<code>axios.post<\/code>\u00a0\u7684 Promise \u51b3\u8bae\uff08\u6210\u529f \/ \u5931\u8d25\uff09\uff0c\u4f46<strong>\u4e3b\u7ebf\u7a0b\u5728\u6b64\u671f\u95f4\u4f1a\u88ab\u91ca\u653e<\/strong>\uff0c\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u4efb\u52a1\uff08\u5982\u65b0\u7684 HTTP \u8bf7\u6c42\u3001\u5b9a\u65f6\u5668\u3001\u6587\u4ef6 I\/O \u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p> \u3000<\/p>\n\n\n\n<p>2. \u5bf9\u6bd4\u793a\u4f8b\uff1a<code>await axios.post<\/code>&nbsp;\u4e0d\u963b\u585e\u5176\u4ed6\u8bf7\u6c42<\/p>\n\n\n\n<p>\u7528 Express \u6a21\u62df\u4e00\u4e2a\u573a\u666f\uff0c\u6e05\u6670\u5c55\u793a&nbsp;<code>await axios.post<\/code>&nbsp;\u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u8bf7\u6c42\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const express = require('express');\nconst axios = require('axios');\nconst app = express();\n\n\/\/ \u6a21\u62df\u4e00\u4e2a\u9700\u8981 await axios.post \u7684\u5f02\u6b65\u63a5\u53e3\napp.get('\/long-axios', async (req, res) => {\n  console.log('\u5f00\u59cb\u5904\u7406\u8bf7\u6c42 A');\n  \n  \/\/ \u5047\u8bbe\u8fd9\u662f\u4e00\u4e2a\u8017\u65f6 3 \u79d2\u7684 HTTP \u8bf7\u6c42\uff08\u6bd4\u5982\u8c03\u7528\u53e6\u4e00\u4e2a\u670d\u52a1\uff09\n  await axios.post('https:\/\/httpbin.org\/delay\/3'); \n  \n  res.send('\u8bf7\u6c42 A \u5b8c\u6210\uff08axios.post \u5df2\u54cd\u5e94\uff09');\n  console.log('\u8bf7\u6c42 A \u5904\u7406\u5b8c\u6bd5');\n});\n\n\/\/ \u6a21\u62df\u4e00\u4e2a\u5feb\u901f\u54cd\u5e94\u7684\u63a5\u53e3\napp.get('\/fast', (req, res) => {\n  res.send('\u8bf7\u6c42 B \u7acb\u5373\u5b8c\u6210');\n  console.log('\u8bf7\u6c42 B \u5904\u7406\u5b8c\u6bd5');\n});\n\napp.listen(3000, () => {\n  console.log('\u670d\u52a1\u5668\u542f\u52a8\uff1ahttp:\/\/localhost:3000');\n});<\/pre>\n\n\n\n<p>\u6267\u884c\u6d41\u7a0b\u4e0e\u7ed3\u679c\uff1a<\/p>\n\n\n\n<ol>\n<li>\u540c\u65f6\u53d1\u9001\u00a0<strong>\u8bf7\u6c42 A<\/strong>\uff08<code>\/long-axios<\/code>\uff09\u548c\u00a0<strong>\u8bf7\u6c42 B<\/strong>\uff08<code>\/fast<\/code>\uff09\uff1a\n<ul>\n<li>\u8bf7\u6c42 A \u6267\u884c\u5230\u00a0<code>await axios.post(...)<\/code>\u00a0\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u91ca\u653e\uff0c\u53bb\u5904\u7406\u8bf7\u6c42 B\u3002<\/li>\n\n\n\n<li>\u8bf7\u6c42 B \u4f1a<strong>\u7acb\u5373\u54cd\u5e94<\/strong>\uff08\u65e0\u9700\u7b49\u5f85\u8bf7\u6c42 A \u7684\u00a0<code>axios.post<\/code>\u00a0\u5b8c\u6210\uff09\u3002<\/li>\n\n\n\n<li>3 \u79d2\u540e\uff0c<code>axios.post<\/code>\u00a0\u54cd\u5e94\uff0c\u8bf7\u6c42 A \u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801\u5e76\u54cd\u5e94\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u63a7\u5236\u53f0\u8f93\u51fa\uff08\u987a\u5e8f\u53ef\u80fd\u56e0\u7f51\u7edc\u7565\u6709\u5dee\u5f02\uff0c\u4f46\u6838\u5fc3\u903b\u8f91\u4e0d\u53d8\uff09\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u5f00\u59cb\u5904\u7406\u8bf7\u6c42 A    \/\/ \u8bf7\u6c42 A \u8fdb\u5165\n\u8bf7\u6c42 B \u5904\u7406\u5b8c\u6bd5    \/\/ \u8bf7\u6c42 B \u88ab\u7acb\u5373\u5904\u7406\n\u8bf7\u6c42 A \u5904\u7406\u5b8c\u6bd5    \/\/ 3 \u79d2\u540e\uff0caxios.post \u5b8c\u6210\uff0c\u8bf7\u6c42 A \u7ee7\u7eed\u6267\u884c<\/pre>\n\n\n\n<p><strong>\u7ed3\u8bba<\/strong>\uff1a<code>await axios.post<\/code>\u00a0\u4e0d\u4f1a\u963b\u585e\u6574\u4e2a\u5e94\u7528\u5904\u7406\u5176\u4ed6\u8bf7\u6c42\uff0c\u53ea\u662f\u6682\u505c\u5f53\u524d\u00a0<code>async<\/code>\u00a0\u51fd\u6570\u7684\u6267\u884c\uff0c\u4e3b\u7ebf\u7a0b\u4ecd\u53ef\u5904\u7406\u65b0\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p> \u3000\u3000<\/p>\n\n\n\n<p>3. \u4e3a\u4ec0\u4e48\u4e0d\u4f1a\u963b\u585e\uff1fNode.js \u7684 \u201c\u5f02\u6b65\u975e\u963b\u585e\u201d \u673a\u5236<\/p>\n\n\n\n<p>Node.js \u4e4b\u6240\u4ee5\u80fd\u9ad8\u6548\u5904\u7406\u9ad8\u5e76\u53d1\uff0c\u4f9d\u8d56&nbsp;<strong>\u4e8b\u4ef6\u5faa\u73af\uff08Event Loop\uff09<\/strong>&nbsp;+&nbsp;<strong>\u7ebf\u7a0b\u6c60\uff08Thread Pool\uff09<\/strong>&nbsp;\u6a21\u578b\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u4e8b\u4ef6\u5faa\u73af<\/strong>\uff1a\u8d1f\u8d23\u8c03\u5ea6 JavaScript \u4ee3\u7801\u7684\u6267\u884c\uff08\u4e3b\u7ebf\u7a0b\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7ebf\u7a0b\u6c60<\/strong>\uff1a\u7531 libuv \u7ba1\u7406\uff0c\u4e13\u95e8\u5904\u7406\u8017\u65f6\u7684 I\/O \u64cd\u4f5c\uff08\u5982\u00a0<code>axios.post<\/code>\u00a0\u7684 HTTP \u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5f53\u6267\u884c&nbsp;<code>axios.post<\/code>&nbsp;\u65f6\uff1a<\/p>\n\n\n\n<ol>\n<li>Node.js \u4f1a\u628a HTTP \u8bf7\u6c42 \u201c\u4ea4\u7ed9\u201d \u7ebf\u7a0b\u6c60\u5904\u7406\uff08<strong>\u4e0d\u5360\u7528\u4e3b\u7ebf\u7a0b<\/strong>\uff09\u3002<\/li>\n\n\n\n<li>\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff08\u5982\u5904\u7406\u65b0\u7684 HTTP \u8bf7\u6c42\u3001\u54cd\u5e94\u5feb\u901f\u63a5\u53e3\u7b49\uff09\u3002<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u6c60\u5b8c\u6210 HTTP \u8bf7\u6c42\u540e\uff0c\u7ed3\u679c\u4f1a\u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af\u901a\u77e5\u4e3b\u7ebf\u7a0b\uff0c<code>await<\/code>\u00a0\u540e\u7684\u4ee3\u7801\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>4. \u4ec0\u4e48\u60c5\u51b5\u4f1a\u771f\u6b63 \u201c\u963b\u585e\u201d\uff1f<\/p>\n\n\n\n<p>\u5982\u679c&nbsp;<code>await<\/code>&nbsp;\u7684\u662f&nbsp;<strong>\u540c\u6b65\u963b\u585e\u64cd\u4f5c<\/strong>\uff08\u800c\u975e\u5f02\u6b65 I\/O\uff09\uff0c\u624d\u4f1a\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">app.get('\/block', async (req, res) => {\n  \/\/ \u7eaf CPU \u8ba1\u7b97\uff0c\u6ca1\u6709\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\uff0c\u4f1a\u5360\u7528\u4e3b\u7ebf\u7a0b\uff01\n  await new Promise((resolve) => {\n    let sum = 0;\n    for (let i = 0; i &lt; 1000000000; i++) {\n      sum += i;\n    }\n    resolve(sum);\n  });\n  res.send('\u7ec8\u4e8e\u7b97\u5b8c\u4e86...');\n});<\/pre>\n\n\n\n<p>\u8fd9\u79cd\u573a\u666f\u4e0b\uff0c<code>for<\/code>\u00a0\u5faa\u73af\u4f1a<strong>\u6301\u7eed\u5360\u7528\u4e3b\u7ebf\u7a0b<\/strong>\uff0c\u671f\u95f4\u6240\u6709\u65b0\u8bf7\u6c42\u90fd\u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u5230\u5faa\u73af\u7ed3\u675f\u3002<\/p>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>5. \u603b\u7ed3<\/p>\n\n\n\n<ul>\n<li><strong><code>await axios.post<\/code>\u00a0\u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u8bf7\u6c42<\/strong>\uff1a<br>\u56e0\u4e3a\u00a0<code>axios.post<\/code>\u00a0\u662f\u5f02\u6b65\u975e\u963b\u585e I\/O \u64cd\u4f5c\uff0c\u4f1a\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u5904\u7406\uff0c\u4e3b\u7ebf\u7a0b\u53ef\u7ee7\u7eed\u54cd\u5e94\u65b0\u8bf7\u6c42\u3002<\/li>\n\n\n\n<li><strong><code>await<\/code>\u00a0\u7684\u4f5c\u7528<\/strong>\uff1a<br>\u4ec5\u6682\u505c\u5f53\u524d\u00a0<code>async<\/code>\u00a0\u51fd\u6570\u7684\u6267\u884c\uff0c\u4e0d\u5f71\u54cd\u6574\u4e2a\u5e94\u7528\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/li>\n\n\n\n<li><strong>\u8b66\u60d5 \u201c\u540c\u6b65\u963b\u585e\u64cd\u4f5c\u201d<\/strong>\uff1a<br>\u5982\u679c\u00a0<code>await<\/code>\u00a0\u7684\u662f\u7eaf CPU \u8ba1\u7b97\uff08\u800c\u975e\u5f02\u6b65 I\/O\uff09\uff0c\u624d\u4f1a\u771f\u6b63\u963b\u585e\u4e3b\u7ebf\u7a0b\uff0c\u5bfc\u81f4\u6240\u6709\u8bf7\u6c42\u6392\u961f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u4f60\u7684\u4e1a\u52a1\u4e2d\u6709\u5927\u91cf\u8017\u65f6\u64cd\u4f5c\uff0c\u5efa\u8bae\uff1a<\/p>\n\n\n\n<ul>\n<li>I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\uff08\u5982\u00a0<code>axios.post<\/code>\uff09\uff1a\u653e\u5fc3\u7528\u00a0<code>async\/await<\/code>\uff0cNode.js \u80fd\u9ad8\u6548\u5904\u7406\u3002<\/li>\n\n\n\n<li>CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff08\u5982\u5927\u5faa\u73af\u3001\u52a0\u5bc6\u8fd0\u7b97\uff09\uff1a\u7528\u00a0<code>worker_threads<\/code>\u00a0\u6216\u62c6\u5206\u6210\u5b50\u8fdb\u7a0b\uff0c\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Node.js \u4e2d\uff0c\u5982\u679c\u4ee3\u7801\u4e2d\u4f7f\u7528\u00a0await\u00a0\u7b49\u5f85\u4e00\u4e2a\u957f\u65f6\u95f4\u64cd\u4f5c\uff08\u5982\u8017\u65f6\u7684\u6570\u636e\u5e93\u67e5\u8be2\u3001\u6587\u4ef6\u8bfb\u5199\u3001API  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/18608"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18608"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/18608\/revisions"}],"predecessor-version":[{"id":18610,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/18608\/revisions\/18610"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}