{"id":589,"date":"2025-07-31T10:25:28","date_gmt":"2025-07-31T02:25:28","guid":{"rendered":"https:\/\/spc.huaweitianli.com\/?p=589"},"modified":"2025-07-31T10:39:26","modified_gmt":"2025-07-31T02:39:26","slug":"promise-async-await","status":"publish","type":"post","link":"https:\/\/www.sunpengcheng.com\/index.php\/2025\/07\/31\/promise-async-await\/","title":{"rendered":"Promise\u4e0easync\/await\u7684\u533a\u522b"},"content":{"rendered":"<p>\u8fd9\u6b21\uff0c\u5f7b\u5e95\u641e\u61c2Promise\u548casync\/await\u3002<\/p>\n<p>Promise\u5df2\u7ecf\u6bd4\u4f20\u7edf\u7684\u56de\u8c03\u51fd\u6570\uff08Callback Hell\uff09\u66f4\u5148\u8fdb\uff0c\u90a3\u540e\u6765\u4e3a\u793e\u4e48\u53c8\u51fa\u73b0async\/await\u4e86\u5462\uff1f<\/p>\n<p>async\/await\u7684\u51fa\u73b0\u662f\u4e3a\u4e86\u8ba9\u5f02\u6b65\u4ee3\u7801\u5199\u8d77\u6765\u66f4\u50cf\u540c\u6b65\u4ee3\u7801\uff0c\u89e3\u51b3Promise\u5728\u590d\u6742\u573a\u666f\u4e0b\u7684\u5d4c\u5957\u95ee\u9898\u548c\u53ef\u8bfb\u6027\u95ee\u9898\uff0c\u8ba9\u6211\u4eec\u80fd\u66f4\u8f7b\u677e\u5730\u7f16\u5199\u548c\u7ef4\u62a4\u5f02\u6b65\u903b\u8f91\u3002<\/p>\n<p>Notes:<br \/>\n*   async\/await \u5e95\u5c42\u4ecd\u7136\u662f Promise\uff0c\u53ea\u662f\u5199\u6cd5\u66f4\u7b80\u6d01\u3002<br \/>\n*   await \u53ea\u80fd\u5728 async \u51fd\u6570\u4e2d\u4f7f\u7528\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<br \/>\n*   async \u51fd\u6570\u603b\u662f\u8fd4\u56de Promise\u3002<br \/>\n*   await \u4f1a\u963b\u585e\u5f53\u524d async \u51fd\u6570\u7684\u6267\u884c\uff0c\u4f46\u4e0d\u4f1a\u963b\u585e\u6574\u4e2a\u7a0b\u5e8f\u3002<\/p>\n<p>\u4e3e\u4f8b\uff1a \u6bd4\u5982\u5165\u95e8Web3\u9879\u76ee\u5f00\u53d1\uff0c\u5fc5\u987b\u8981\u5148\u5b66\u4f1aNext.js\u3001ethers.js\u3001Solidity\u3001Go, \u624d\u80fd\u505a\u667a\u80fd\u5408\u7ea6\u5f00\u53d1\u3002\u57fa\u7840\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>function learnNextJs() {\n    return new Promise((resolve) => {\n        setTimeout(() => {\n            console.log(\"1. \u5df2\u5b8c\u6210 Next.js \u5b66\u4e60\");\n            resolve();\n        }, 1000);\n    });\n}\n\nfunction learnEthersJs() {\n    return new Promise((resolve) => {\n        setTimeout(() => {\n            console.log(\"2. \u5df2\u5b8c\u6210 ethers.js \u5b66\u4e60\");\n            resolve();\n        }, 1500);\n    });\n}\n\nfunction learnSolidity() {\n    return new Promise((resolve) => {\n        setTimeout(() => {\n            console.log(\"3. \u5df2\u5b8c\u6210 Solidity \u5b66\u4e60\");\n            resolve();\n        }, 2000);\n    });\n}\n\nfunction learnGo() {\n    return new Promise((resolve) => {\n        setTimeout(() => {\n            console.log(\"4. \u5df2\u5b8c\u6210 Go \u5b66\u4e60\uff08\u53ef\u9009\uff09\");\n            resolve();\n        }, 500);\n    });\n}\n<\/pre>\n<p>\u7136\u540e\u4f7f\u7528 Promise \u94fe\u5f0f\u8c03\u7528\uff08\u5fc5\u987b\u6309\u987a\u5e8f\u5b66\u4e60\uff09<\/p>\n<pre>learnNextJs()\n.then(() => learnEthersJs())\n.then(() => learnSolidity())\n.then(() => learnGo())\n.then(() => {\n    console.log(\"\u606d\u559c\uff01\u4f60\u73b0\u5728\u53ef\u4ee5\u5f00\u53d1\u5b8c\u6574\u7684 Web3 \u9879\u76ee\u4e86\uff01\");\n})\n.catch((error) => {\n    console.log(\"\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u51fa\u9519:\", error);\n});\n<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u662f\uff1a<br \/>\n1&#46; \u5df2\u5b8c\u6210 Next.js \u5b66\u4e60<br \/>\n2&#46; \u5df2\u5b8c\u6210 ethers.js \u5b66\u4e60<br \/>\n3&#46; \u5df2\u5b8c\u6210 Solidity \u5b66\u4e60<br \/>\n4&#46; \u5df2\u5b8c\u6210 Go \u5b66\u4e60\uff08\u53ef\u9009\uff09<br \/>\n\u606d\u559c\uff01\u4f60\u73b0\u5728\u53ef\u4ee5\u5f00\u53d1\u5b8c\u6574\u7684 Web3 \u9879\u76ee\u4e86\uff01<\/p>\n<p>\u90a3\u5982\u679c\u6362\u6210async\/await\u65b9\u5f0f\u5462\uff1f<\/p>\n<pre>async function web3LearningPath() {\n    try {\n        \/\/ \u5fc5\u987b\u6309\u987a\u5e8f\u5b66\u4e60\n        await learnNextJs();       \/\/ \u7b49\u5f85\u7b2c\u4e00\u6b65\u5b8c\u6210\n        await learnEthersJs();    \/\/ \u7136\u540e\u5b66\u4e60\u7b2c\u4e8c\u6b65\n        await learnSolidity();    \/\/ \u63a5\u7740\u5b66\u4e60\u7b2c\u4e09\u6b65\n        \n        \/\/ Go \u662f\u53ef\u9009\u7684\uff0c\u53ef\u4ee5\u5e76\u884c\u5b66\u4e60\n        const optionalLearning = learnGo(); \/\/ \u4e0d\u7b49\u5f85\uff0c\u7ee7\u7eed\u6267\u884c\u540e\u9762\u4ee3\u7801\n        \n        console.log(\"\u6b63\u5728\u540c\u65f6\u5b66\u4e60 Solidity \u548c Go...\");\n        \n        await optionalLearning; \/\/ \u5982\u679c\u9700\u8981\u786e\u4fddGo\u4e5f\u5b66\u5b8c\u624d\u7ee7\u7eed\n        \n        console.log(\"\u606d\u559c\uff01\u4f60\u73b0\u5728\u53ef\u4ee5\u5f00\u53d1\u5b8c\u6574\u7684 Web3 \u9879\u76ee\u4e86\uff01\");\n        \n        \/\/ \u73b0\u5728\u53ef\u4ee5\u5f00\u59cb\u667a\u80fd\u5408\u7ea6\u5f00\u53d1\u4e86\n        await startSmartContractDevelopment();\n    } catch (error) {\n        console.log(\"\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u51fa\u9519:\", error);\n    }\n}\n\n\/\/ \u542f\u52a8\u5b66\u4e60\u6d41\u7a0b\nweb3LearningPath();\n\nasync function startSmartContractDevelopment() {\n    console.log(\"\u5f00\u59cb\u667a\u80fd\u5408\u7ea6\u5f00\u53d1...\");\n}\n<\/pre>\n<p>\u5219\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<br \/>\n1&#46; \u5df2\u5b8c\u6210 Next.js \u5b66\u4e60<br \/>\n2&#46; \u5df2\u5b8c\u6210 ethers.js \u5b66\u4e60<br \/>\n3&#46; \u5df2\u5b8c\u6210 Solidity \u5b66\u4e60<br \/>\n\u6b63\u5728\u540c\u65f6\u5b66\u4e60 Solidity \u548c Go&#8230;<br \/>\n4&#46; \u5df2\u5b8c\u6210 Go \u5b66\u4e60\uff08\u53ef\u9009\uff09<br \/>\n\u606d\u559c\uff01\u4f60\u73b0\u5728\u53ef\u4ee5\u5f00\u53d1\u5b8c\u6574\u7684 Web3 \u9879\u76ee\u4e86\uff01<br \/>\n\u5f00\u59cb\u667a\u80fd\u5408\u7ea6\u5f00\u53d1&#8230;<\/p>\n<p>\u6700\u540e\uff1a<br \/>\nPromise\u662f\u57fa\u7840\uff0c\u6240\u6709\u5f02\u6b65\u64cd\u4f5c\u6700\u7ec8\u90fd\u662fPromise\uff0casync\/await\u662f\u66f4\u4f18\u96c5\u7684\u5199\u6cd5\uff0c\u8ba9\u5f02\u6b65\u4ee3\u7801\u66f4\u6613\u8bfb\u3002\u5728 Web3 \u5f00\u53d1\u4e2d\uff0c\u5927\u90e8\u5206\u60c5\u51b5\u5e94\u4f7f\u7528async\/await\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\uff0c\u5f7b\u5e95\u641e\u61c2Promise\u548casync\/await\u3002 Promise\u5df2\u7ecf\u6bd4\u4f20\u7edf\u7684\u56de\u8c03\u51fd\u6570\uff08Callback <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/www.sunpengcheng.com\/index.php\/2025\/07\/31\/promise-async-await\/\">Continue Reading&hellip;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,67],"tags":[],"class_list":["post-589","post","type-post","status-publish","format-standard","hentry","category-js","category-web3"],"_links":{"self":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts\/589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/comments?post=589"}],"version-history":[{"count":17,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts\/589\/revisions"}],"predecessor-version":[{"id":606,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts\/589\/revisions\/606"}],"wp:attachment":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/media?parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/categories?post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/tags?post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}