{"id":235235,"date":"2024-05-12T10:16:30","date_gmt":"2024-05-12T02:16:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/235235.html"},"modified":"2024-05-12T10:16:39","modified_gmt":"2024-05-12T02:16:39","slug":"javascript%e5%a6%82%e4%bd%95%e5%85%bc%e5%ae%b9%e6%80%a7%e6%9c%80%e5%a5%bd%e5%9c%b0%e5%88%a4%e6%96%ad%e6%98%af%e5%90%a6%e6%98%af%e4%b8%80%e4%b8%aaasync%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/235235.html","title":{"rendered":"Javascript\u5982\u4f55\u517c\u5bb9\u6027\u6700\u597d\u5730\u5224\u65ad\u662f\u5426\u662f\u4e00\u4e2aAsync\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26234127\/eb7645f5-f41d-408c-910f-5c808b90a0a3.webp\" alt=\"Javascript\u5982\u4f55\u517c\u5bb9\u6027\u6700\u597d\u5730\u5224\u65ad\u662f\u5426\u662f\u4e00\u4e2aAsync\u51fd\u6570\" \/><\/p>\n<p><p>\u5224\u65adJavaScript\u4e2d\u4e00\u4e2a\u51fd\u6570\u662f\u5426\u662f\u5f02\u6b65\u51fd\u6570(Async\u51fd\u6570)\uff0c\u5173\u952e\u5728\u4e8e\u7406\u89e3Async\u51fd\u6570\u7684\u7279\u6027\uff0c\u4ee5\u53ca\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u5982\u4f55\u6b63\u786e\u8bc6\u522b\u8fd9\u7c7b\u51fd\u6570\u7684\u7279\u5f81\u3002\u6838\u5fc3\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528<code>Object.prototype.toString.call<\/code>\u65b9\u6cd5\u3001\u68c0\u67e5\u51fd\u6570\u7684\u6784\u9020\u51fd\u6570\u3001\u4ee5\u53ca\u5229\u7528<code>async<\/code>\u51fd\u6570\u7684\u9ed8\u8ba4\u8fd4\u56de\u503c\u7279\u6027\u7b49\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528<code>Object.prototype.toString.call<\/code>\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u4fbf\uff0c\u800c\u4e14\u517c\u5bb9\u6027\u826f\u597d\uff0c\u80fd\u591f\u5728\u4e0d\u540c\u6267\u884c\u73af\u5883\u4e0b\u63d0\u4f9b\u51c6\u786e\u7684\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><p>\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\uff1a<code>Object.prototype.toString.call<\/code>\u65b9\u6cd5\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u7684<code>toString()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u5176\u5185\u90e8<code>[[Class]]<\/code>\u5c5e\u6027\uff0c\u4ece\u800c\u51c6\u786e\u5224\u65ad\u51fa\u8be5\u51fd\u6570\u7684\u7c7b\u578b\u3002\u5bf9\u4e8e\u5f02\u6b65\u51fd\u6570\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u8fd4\u56de<code>&quot;[object AsyncFunction]&quot;<\/code>\uff0c\u8fd9\u662f\u4e00\u4e2a\u660e\u786e\u4e14\u53ef\u9760\u7684\u6807\u5fd7\uff0c\u8868\u660e\u4e86\u7ed9\u5b9a\u7684\u51fd\u6570\u662f\u4e00\u4e2aAsync\u51fd\u6570\u3002\u76f8\u8f83\u4e8e\u5176\u4ed6\u65b9\u6cd5\uff0c\u8fd9\u4e00\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u8de8\u73af\u5883\u7684\u517c\u5bb9\u6027\uff0c\u4f7f\u5f97\u5b83\u6210\u4e3a\u5224\u65ad\u51fd\u6570\u662f\u5426\u4e3aAsync\u51fd\u6570\u7684\u9996\u9009\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5229\u7528OBJECT.PROTOTYPE.TOSTRING.CALL\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>Object.prototype.toString.call<\/code>\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u3001\u6700\u517c\u5bb9\u7684\u65b9\u5f0f\u4e4b\u4e00\u6765\u5224\u65ad\u4e00\u4e2a\u51fd\u6570\u662f\u5426\u662fAsync\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u5229\u7528\u4e86JavaScript\u5185\u7f6e\u7684\u5bf9\u8c61\u6807\u7b7e\u673a\u5236\uff0c\u901a\u8fc7\u8be5\u65b9\u6cd5\u53ef\u4ee5\u51c6\u786e\u5730\u8bc6\u522b\u51fa\u51fd\u6570\u7684\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u5982\u4f55\u5e94\u7528\u8fd9\u79cd\u65b9\u6cd5\uff1f<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bf9\u7591\u4f3c\u7684\u5f02\u6b65\u51fd\u6570\u5e94\u7528<code>Object.prototype.toString.call<\/code>\u65b9\u6cd5\u3002\u5982\u679c\u7ed3\u679c\u662f<code>&quot;[object AsyncFunction]&quot;<\/code>\uff0c\u90a3\u4e48\u53ef\u4ee5\u786e\u8ba4\u5b83\u662f\u4e00\u4e2aAsync\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u51e0\u4e4e\u5728\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u548cNode.js\u73af\u5883\u4e2d\u90fd\u6709\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u5176\u4f18\u52bf<\/h3>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u7684\u6700\u5927\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u6d01\u548c\u517c\u5bb9\u6027\u3002\u5b83\u4e0d\u4f9d\u8d56\u4e8e\u4efb\u4f55\u7b2c\u4e09\u65b9\u5e93\uff0c\u4e5f\u4e0d\u9700\u8981\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\u6216\u662f\u7279\u5b9a\u7684\u73af\u5883\u914d\u7f6e\uff0c\u53ef\u4ee5\u8bf4\u662f\u201c\u5f00\u7bb1\u5373\u7528\u201d\u7684\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u68c0\u67e5\u51fd\u6570\u7684CONSTRUCTOR\u5c5e\u6027<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u68c0\u67e5\u51fd\u6570\u7684<code>constructor<\/code>\u5c5e\u6027\u3002<code>AsyncFunction<\/code>\u6784\u9020\u5668\u662f\u6240\u6709\u5f02\u6b65\u51fd\u6570\u7684\u57fa\u7840\uff0c\u56e0\u6b64\u901a\u8fc7\u68c0\u67e5\u51fd\u6570\u7684<code>constructor<\/code>\u5c5e\u6027\uff0c\u4e5f\u80fd\u591f\u5224\u65ad\u51fa\u662f\u5426\u5f02\u6b65\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5982\u4f55\u64cd\u4f5c\uff1f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u68c0\u67e5\u76ee\u6807\u51fd\u6570\u7684<code>constructor.name<\/code>\u5c5e\u6027\u662f\u5426\u7b49\u4e8e<code>&quot;AsyncFunction&quot;<\/code>\u6765\u5224\u65ad\u3002\u5982\u679c\u662f\uff0c\u90a3\u4e48\u8be5\u51fd\u6570\u5373\u4e3a\u5f02\u6b65\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u8fd9\u4e2a\u65b9\u6cd5\u8f83\u4e3a\u76f4\u63a5\uff0c\u4f46\u5728\u67d0\u4e9bJavaScript\u73af\u5883\u4e2d\uff08\u7279\u522b\u662f\u65e9\u671f\u7684JavaScript\u7248\u672c\uff09\uff0c<code>constructor.name<\/code>\u5c5e\u6027\u53ef\u80fd\u4f1a\u88ab\u538b\u7f29\u5de5\u5177\u4fee\u6539\uff0c\u5bfc\u81f4\u65e0\u6cd5\u51c6\u786e\u5224\u65ad\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u65f6\u9700\u8981\u8003\u8651\u73af\u5883\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5f02\u6b65\u51fd\u6570\u7684\u9ed8\u8ba4\u8fd4\u56de\u503c\u7279\u6027<\/h2>\n<\/p>\n<p><p>\u56e0\u4e3aAsync\u51fd\u6570\u9ed8\u8ba4\u8fd4\u56de\u4e00\u4e2a<code>Promise<\/code>\u5bf9\u8c61\uff0c\u6240\u4ee5\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u51fd\u6570\u6267\u884c\u7684\u8fd4\u56de\u503c\u662f\u5426\u662f<code>Promise<\/code>\u5bf9\u8c61\u6765\u5224\u65ad\u5b83\u662f\u5426\u4e3a\u5f02\u6b65\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5b9e\u73b0\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c1d\u8bd5\u6267\u884c\u51fd\u6570\u5e76\u68c0\u67e5\u8fd4\u56de\u503c\u3002\u5982\u679c\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u4e14\u8be5\u5bf9\u8c61\u5177\u6709<code>then<\/code>\u548c<code>catch<\/code>\u65b9\u6cd5\uff0c\u90a3\u4e48\u5f88\u53ef\u80fd\u8fd9\u662f\u4e00\u4e2a<code>Promise<\/code>\u5bf9\u8c61\uff0c\u4ece\u800c\u53ef\u4ee5\u63a8\u65ad\u51fa\u8be5\u51fd\u6570\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u5b9e\u9645\u6267\u884c\u51fd\u6570\uff0c\u53ef\u80fd\u4f1a\u5f15\u8d77\u4e0d\u5fc5\u8981\u7684\u526f\u4f5c\u7528\u3002\u56e0\u6b64\uff0c\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u79cd\u8865\u5145\u624b\u6bb5\uff0c\u4f46\u4e0d\u63a8\u8350\u4f5c\u4e3a\u4e3b\u8981\u5224\u65ad\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3\u4e0e\u63a8\u8350<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u8df5\u4e2d\uff0c\u4f7f\u7528<code>Object.prototype.toString.call<\/code>\u65b9\u6cd5\u5224\u65ad\u662f\u5426\u4e3a\u5f02\u6b65\u51fd\u6570\u662f\u6700\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u65e2\u7b80\u5355\u53c8\u517c\u5bb9\u5e7f\u6cdb\u7684\u6267\u884c\u73af\u5883\u3002\u53e6\u5916\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f5c\u4e3a\u8865\u5145\u6216\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u4e86\u89e3\u5f02\u6b65\u51fd\u6570\u7684\u5185\u90e8\u673a\u5236\u548cJavaScript\u73af\u5883\u7684\u7279\u6027\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5728\u7f16\u5199\u517c\u5bb9\u6027\u4ee3\u7801\u65f6\uff0c\u8003\u8651\u5230\u4e0d\u540c\u73af\u5883\u7684\u7279\u6027\u548c\u9650\u5236\uff0c\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u65b9\u6cd5\u6765\u786e\u4fdd\u4ee3\u7801\u7684\u7a33\u5065\u6027\u548c\u53ef\u9760\u6027\uff0c\u662f\u6bcf\u4e00\u4e2aJavaScript\u5f00\u53d1\u8005\u5fc5\u987b\u9762\u5bf9\u7684\u6311\u6218\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u51fd\u6570\u662f\u5426\u4e3aAsync\u51fd\u6570\uff1f<\/strong><br \/>\n\u8981\u5224\u65ad\u4e00\u4e2a\u51fd\u6570\u662f\u5426\u4e3aAsync\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528Instanceof\u8fd0\u7b97\u7b26\u548cAsyncFunction\u6784\u9020\u51fd\u6570\u6765\u8fdb\u884c\u5224\u65ad\u3002AsyncFunction\u6784\u9020\u51fd\u6570\u662f\u5f02\u6b65\u51fd\u6570\u7684\u6784\u9020\u51fd\u6570\uff0c\u901a\u8fc7\u4f7f\u7528instanceof\u5c06\u8981\u5224\u65ad\u7684\u51fd\u6570\u4e0eAsyncFunction\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u8fd4\u56de\u503c\u4e3atrue\uff0c\u5219\u8bf4\u660e\u8be5\u51fd\u6570\u662f\u4e00\u4e2aAsync\u51fd\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">function isAsyncFunction(func) {\n  return func instanceof AsyncFunction;\n}\n\n\/\/ Example usage\nlet asyncFunc = async () =&gt; {\n  \/\/ Code implementation\n};\n\nlet regularFunc = () =&gt; {\n  \/\/ Code implementation\n};\n\nconsole.log(isAsyncFunction(asyncFunc)); \/\/ true\nconsole.log(isAsyncFunction(regularFunc)); \/\/ false\n<\/code><\/pre>\n<p><strong>2. \u662f\u5426\u5b58\u5728\u5176\u4ed6\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u51fd\u6570\u662f\u5426\u4e3aAsync\u51fd\u6570\uff1f<\/strong><br \/>\n\u9664\u4e86\u4f7f\u7528Instanceof\u8fd0\u7b97\u7b26\u548cAsyncFunction\u6784\u9020\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u51fd\u6570\u662f\u5426\u4e3aAsync\u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u7684toString\u65b9\u6cd5\uff0c\u5c06\u51fd\u6570\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u68c0\u67e5\u51fd\u6570\u4f53\u662f\u5426\u5305\u542b&quot;async&quot;\u5173\u952e\u5b57\u3002\u5982\u679c\u5305\u542b\uff0c\u5219\u8bf4\u660e\u8be5\u51fd\u6570\u662f\u4e00\u4e2aAsync\u51fd\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">function isAsyncFunction(func) {\n  let funcAsString = func.toString();\n  return funcAsString.includes(&quot;async&quot;);\n}\n\n\/\/ Example usage\nlet asyncFunc = async () =&gt; {\n  \/\/ Code implementation\n};\n\nlet regularFunc = () =&gt; {\n  \/\/ Code implementation\n};\n\nconsole.log(isAsyncFunction(asyncFunc)); \/\/ true\nconsole.log(isAsyncFunction(regularFunc)); \/\/ false\n<\/code><\/pre>\n<p><strong>3. \u600e\u6837\u517c\u5bb9\u6027\u6700\u597d\u5730\u5224\u65ad\u4e00\u4e2a\u51fd\u6570\u662f\u5426\u4e3aAsync\u51fd\u6570\uff1f<\/strong><br \/>\n\u517c\u5bb9\u6027\u6700\u597d\u7684\u65b9\u6cd5\u662f\u4f7f\u7528try-catch\u5757\u3002\u5728\u51fd\u6570\u4f53\u5185\u90e8\uff0c\u4f7f\u7528try-catch\u5757\u6765\u6267\u884caw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\u5173\u952e\u5b57\u3002\u5982\u679c\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u629b\u51fa\u5f02\u5e38\uff0c\u5219\u8bf4\u660e\u8be5\u51fd\u6570\u4e0d\u662fAsync\u51fd\u6570\u3002\u5426\u5219\uff0c\u5219\u8bf4\u660e\u8be5\u51fd\u6570\u662fAsync\u51fd\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">function isAsyncFunction(func) {\n  try {\n    await func();\n    return true;\n  } catch (e) {\n    return false;\n  }\n}\n\n\/\/ Example usage\nlet asyncFunc = async () =&gt; {\n  \/\/ Code implementation\n};\n\nlet regularFunc = () =&gt; {\n  \/\/ Code implementation\n};\n\nconsole.log(isAsyncFunction(asyncFunc)); \/\/ true\nconsole.log(isAsyncFunction(regularFunc)); \/\/ false\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5224\u65adJavaScript\u4e2d\u4e00\u4e2a\u51fd\u6570\u662f\u5426\u662f\u5f02\u6b65\u51fd\u6570(Async\u51fd\u6570)\uff0c\u5173\u952e\u5728\u4e8e\u7406\u89e3Async\u51fd\u6570\u7684\u7279\u6027\uff0c\u4ee5\u53ca\u5728\u4e0d [&hellip;]","protected":false},"author":3,"featured_media":235247,"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\/235235"}],"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=235235"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/235235\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/235247"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=235235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=235235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=235235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}