{"id":446,"date":"2025-04-10T15:34:47","date_gmt":"2025-04-10T07:34:47","guid":{"rendered":"https:\/\/blog.laning.org\/?p=446"},"modified":"2025-04-10T15:34:47","modified_gmt":"2025-04-10T07:34:47","slug":"%e5%bc%82%e6%ad%a5%e5%87%bd%e6%95%b0async%e5%92%8cawait","status":"publish","type":"post","link":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/","title":{"rendered":"\u5f02\u6b65\u51fd\u6570async\u548cawait"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<p>\u6700\u8fd1\u5728\u56de\u5934\u770b<a href=\"https:\/\/github.com\/laningya\/nfd\/blob\/main\/worker.js\">nfd\u9879\u76ee<\/a> \u7684\u6e90\u7801\uff0c\u770b\u5230\u4e86\u4f7f\u7528async\u548cawait\u7684js\u4ee3\u7801\uff0c\u56e0\u6b64\u60f3\u4e86\u89e3\u4e00\u4e0b\u4ee3\u7801\u8fd0\u884c\u7684\u673a\u5236\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u4e2a\u4f8b\u5b50<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>function SicBo() {<br>&nbsp;return new Promise((resolve, reject) =&gt; {<br>&nbsp; &nbsp;console.log(\"\u5f00\u59cb\u6447\")<br>&nbsp; &nbsp;setTimeout(() =&gt; {<br>&nbsp; &nbsp; &nbsp;let n = parseInt(Math.random() * 6 + 1, 10)<br>&nbsp; &nbsp; &nbsp;resolve(n)<br>&nbsp; }, 1000)<br>})<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e0a\u8ff0\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/h3>\n\n\n\n<ul>\n<li><strong>\u7acb\u5373\u6267\u884c <code>console.log(\"\u5f00\u59cb\u6447\")<\/code><\/strong> \u2014\u2014 \u2705 \u9a6c\u4e0a\u8f93\u51fa\u3002<\/li>\n\n\n\n<li><strong>\u542f\u52a8\u4e00\u4e2a\u201c1\u79d2\u540e\u6267\u884c\u7684\u4efb\u52a1\u201d<\/strong> \u2014\u2014 \u901a\u8fc7 <code>setTimeout(...)<\/code> \u6ce8\u518c\u4e86\u4e00\u4e2a\u56de\u8c03\uff0c1 \u79d2\u540e\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u4e00\u4e2a Promise \u5bf9\u8c61<\/strong>\uff0c\u8fd9\u4e2a Promise \u4f1a\u5728 1 \u79d2\u540e\u53d8\u6210\u201c\u5b8c\u6210\u72b6\u6001\u201d\u5e76\u7ed9\u51fa\u70b9\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u679c\u76f4\u63a5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function test() {<br>&nbsp;const result = SicBo()<br>&nbsp;console.log('\u9ab0\u5b50\u7ed3\u679c:', result)<br>}<br>test()<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5f00\u59cb\u6447<br>\u9ab0\u5b50\u7ed3\u679c: Promise { &lt;pending> }<\/code><\/pre>\n\n\n\n<p>\u663e\u7136\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u7ed3\u679c<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f02\u6b65\u51fd\u6570\u7684\u4e00\u822c\u8c03\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SicBo().then(result =&gt; {<br>&nbsp;console.log('\u9ab0\u5b50\u7ed3\u679c:', result)<br>})<\/code><\/pre>\n\n\n\n<p>\u8fd9\u79cd\u5199\u6cd5\u6700\u5927\u7684\u5f0a\u7aef\u662f\u4f1a\u4ea7\u751f\u56de\u8c03\u5730\u72f1\uff0c\u5373\u8c03\u7528\u591a\u4e2a\u5f02\u6b65\u51fd\u6570\u4ea7\u751f\u5d4c\u5957\uff0c\u5bfc\u81f4\u4ee3\u7801\u53ef\u8bfb\u6027\u53d8\u5dee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f02\u6b65\u51fd\u6570\u7684async\u548cawait\u8c03\u7528\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>async function test() {<br>&nbsp;const result = await SicBo()<br>&nbsp;console.log('\u9ab0\u5b50\u7ed3\u679c:', result)<br>}<br>test()<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5f00\u59cb\u6447<br>\uff08\u7b49\u5f851\u79d2\uff09<br>\u9ab0\u5b50\u7ed3\u679c: 3<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f02\u6b65\u51fd\u6570\u5916\u90e8\u8c03\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>async function demo() {<br>&nbsp;console.log('A. \u5f00\u59cb')<br>&nbsp;await SicBo()<br>&nbsp;console.log('B. \u7ed3\u675f')<br>}<br>\u200b<br>demo()<br>console.log('C. \u6211\u662f\u5176\u4ed6\u903b\u8f91')<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A. \u5f00\u59cb<br>\u5f00\u59cb\u6447<br>C. \u6211\u662f\u5176\u4ed6\u903b\u8f91<br>\uff08\u7b49\u5f851\u79d2\uff09<br>B. \u7ed3\u675f<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u6700\u8fd1\u5728\u56de\u5934\u770bnfd\u9879\u76ee \u7684\u6e90\u7801\uff0c\u770b\u5230\u4e86\u4f7f\u7528async\u548cawait\u7684js\u4ee3\u7801\uff0c\u56e0\u6b64\u60f3\u4e86\u89e3\u4e00\u4e0b\u4ee3\u7801\u8fd0\u884c\u7684\u673a\u5236 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v17.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5f02\u6b65\u51fd\u6570async\u548cawait &ndash; Laning &#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5f02\u6b65\u51fd\u6570async\u548cawait &ndash; Laning &#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"\u80cc\u666f \u6700\u8fd1\u5728\u56de\u5934\u770bnfd\u9879\u76ee \u7684\u6e90\u7801\uff0c\u770b\u5230\u4e86\u4f7f\u7528async\u548cawait\u7684js\u4ee3\u7801\uff0c\u56e0\u6b64\u60f3\u4e86\u89e3\u4e00\u4e0b\u4ee3\u7801\u8fd0\u884c\u7684\u673a\u5236 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/\" \/>\n<meta property=\"og:site_name\" content=\"Laning &#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-10T07:34:47+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"laning\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1\u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.laning.org\/#website\",\"url\":\"https:\/\/blog.laning.org\/\",\"name\":\"\\u8fa3\\u5b81\\u7684\\u535a\\u5ba2\",\"description\":\"Learning on the go\",\"publisher\":{\"@id\":\"https:\/\/blog.laning.org\/#\/schema\/person\/040331b403ed00370f2fc1e8eb6e6b93\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.laning.org\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#webpage\",\"url\":\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/\",\"name\":\"\\u5f02\\u6b65\\u51fd\\u6570async\\u548cawait &ndash; Laning &#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.laning.org\/#website\"},\"datePublished\":\"2025-04-10T07:34:47+00:00\",\"dateModified\":\"2025-04-10T07:34:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#breadcrumb\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"https:\/\/blog.laning.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\\u5f02\\u6b65\\u51fd\\u6570async\\u548cawait\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#webpage\"},\"author\":{\"@id\":\"https:\/\/blog.laning.org\/#\/schema\/person\/040331b403ed00370f2fc1e8eb6e6b93\"},\"headline\":\"\\u5f02\\u6b65\\u51fd\\u6570async\\u548cawait\",\"datePublished\":\"2025-04-10T07:34:47+00:00\",\"dateModified\":\"2025-04-10T07:34:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#webpage\"},\"wordCount\":10,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blog.laning.org\/#\/schema\/person\/040331b403ed00370f2fc1e8eb6e6b93\"},\"articleSection\":[\"Linux\"],\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blog.laning.org\/#\/schema\/person\/040331b403ed00370f2fc1e8eb6e6b93\",\"name\":\"laning\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/blog.laning.org\/#personlogo\",\"inLanguage\":\"zh-CN\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/17215f328425d53537532588320693a1?s=96&d=retro&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/17215f328425d53537532588320693a1?s=96&d=retro&r=g\",\"caption\":\"laning\"},\"logo\":{\"@id\":\"https:\/\/blog.laning.org\/#personlogo\"},\"sameAs\":[\"https:\/\/blog.laning.org\"],\"url\":\"https:\/\/blog.laning.org\/index.php\/author\/laning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5f02\u6b65\u51fd\u6570async\u548cawait &ndash; Laning &#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/","og_locale":"zh_CN","og_type":"article","og_title":"\u5f02\u6b65\u51fd\u6570async\u548cawait &ndash; Laning &#039;s Blog","og_description":"\u80cc\u666f \u6700\u8fd1\u5728\u56de\u5934\u770bnfd\u9879\u76ee \u7684\u6e90\u7801\uff0c\u770b\u5230\u4e86\u4f7f\u7528async\u548cawait\u7684js\u4ee3\u7801\uff0c\u56e0\u6b64\u60f3\u4e86\u89e3\u4e00\u4e0b\u4ee3\u7801\u8fd0\u884c\u7684\u673a\u5236 [&hellip;]","og_url":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/","og_site_name":"Laning &#039;s Blog","article_published_time":"2025-04-10T07:34:47+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"laning","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1\u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/blog.laning.org\/#website","url":"https:\/\/blog.laning.org\/","name":"\u8fa3\u5b81\u7684\u535a\u5ba2","description":"Learning on the go","publisher":{"@id":"https:\/\/blog.laning.org\/#\/schema\/person\/040331b403ed00370f2fc1e8eb6e6b93"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.laning.org\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-CN"},{"@type":"WebPage","@id":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#webpage","url":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/","name":"\u5f02\u6b65\u51fd\u6570async\u548cawait &ndash; Laning &#039;s Blog","isPartOf":{"@id":"https:\/\/blog.laning.org\/#website"},"datePublished":"2025-04-10T07:34:47+00:00","dateModified":"2025-04-10T07:34:47+00:00","breadcrumb":{"@id":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#breadcrumb"},"inLanguage":"zh-CN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/blog.laning.org\/"},{"@type":"ListItem","position":2,"name":"\u5f02\u6b65\u51fd\u6570async\u548cawait"}]},{"@type":"Article","@id":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#article","isPartOf":{"@id":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#webpage"},"author":{"@id":"https:\/\/blog.laning.org\/#\/schema\/person\/040331b403ed00370f2fc1e8eb6e6b93"},"headline":"\u5f02\u6b65\u51fd\u6570async\u548cawait","datePublished":"2025-04-10T07:34:47+00:00","dateModified":"2025-04-10T07:34:47+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#webpage"},"wordCount":10,"commentCount":0,"publisher":{"@id":"https:\/\/blog.laning.org\/#\/schema\/person\/040331b403ed00370f2fc1e8eb6e6b93"},"articleSection":["Linux"],"inLanguage":"zh-CN","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.laning.org\/index.php\/2025\/04\/10\/446\/#respond"]}]},{"@type":["Person","Organization"],"@id":"https:\/\/blog.laning.org\/#\/schema\/person\/040331b403ed00370f2fc1e8eb6e6b93","name":"laning","image":{"@type":"ImageObject","@id":"https:\/\/blog.laning.org\/#personlogo","inLanguage":"zh-CN","url":"https:\/\/secure.gravatar.com\/avatar\/17215f328425d53537532588320693a1?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/17215f328425d53537532588320693a1?s=96&d=retro&r=g","caption":"laning"},"logo":{"@id":"https:\/\/blog.laning.org\/#personlogo"},"sameAs":["https:\/\/blog.laning.org"],"url":"https:\/\/blog.laning.org\/index.php\/author\/laning\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/posts\/446"}],"collection":[{"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/comments?post=446"}],"version-history":[{"count":1,"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/posts\/446\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.laning.org\/index.php\/wp-json\/wp\/v2\/tags?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}