{"id":30296,"date":"2024-11-25T13:27:45","date_gmt":"2024-11-25T05:27:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/30296\/"},"modified":"2024-11-25T13:27:45","modified_gmt":"2024-11-25T05:27:45","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%bc%80%e5%8f%91%e4%b9%8b%e5%88%a9%e7%94%a8co%e5%a4%84%e7%90%86%e5%bc%82%e6%ad%a5%e6%b5%81%e7%a8%8b%e7%9a%84%e5%ae%9e%e4%be%8b%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/30296\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5229\u7528co\u5904\u7406\u5f02\u6b65\u6d41\u7a0b\u7684\u5b9e\u4f8b\u6559\u7a0b"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u5b66\u4e60\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\uff0c\u4e0b\u9762\u5c31\u5b66\u4e60\u7684\u5185\u5bb9\u8fdb\u884c\u603b\u7ed3\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5229\u7528co\u5904\u7406\u5f02\u6b65\u6d41\u7a0b\u7684\u65b9\u6cd5\uff0c\u6587\u4e2d\u7ed9\u51fa\u4e86\u8be6\u7ec6\u7684\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\u4f9b\u5927\u5bb6\u53c2\u8003\u5b66\u4e60\uff0c\u9700\u8981\u7684\u670b\u53cb\u4eec\u4e0b\u9762\u6765\u4e00\u8d77\u770b\u770b\u5427\u3002<\/p>\n<p>\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u7684\u662f\u5173\u4e8e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5229\u7528co\u5904\u7406\u5f02\u6b65\u6d41\u7a0b\u7684\u65b9\u6cd5\u6559\u7a0b\uff0c\u5206\u4eab\u51fa\u6765\u4f9b\u5927\u5bb6\u53c2\u8003\u5b66\u4e60\uff0c\u9700\u8981\u7684\u670b\u53cb\u4eec\u4e0b\u9762\u6765\u770b\u770b\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a<\/p>\n<p><span style=\"color: #ff0000\"><strong>co<\/strong><\/span><\/p>\n<p>co\u662f\u4e00\u4e2a\u57fa\u4e8eES6 Generator\u7279\u6027\u5b9e\u73b0\u7684\u3010\u5f02\u6b65\u6d41\u7a0b\u540c\u6b65\u5316\u3011\u5199\u6cd5\u7684\u5de5\u5177\u5e93\u3002<\/p>\n<p>co\u9700\u8981\u4f7f\u7528Promise\u7279\u6027\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u5148\u6765\u521b\u5efa\u4e00\u4e2a\u4f7f\u7528Promise\u6765\u5904\u7406\u7684\u5f02\u6b65\u65b9\u6cd5\uff1a<\/p>\n<pre>function&nbsp;myAsyncFunc()&nbsp;{\r\n&nbsp;return&nbsp;new&nbsp;Promise(function&nbsp;(resolve,&nbsp;reject)&nbsp;{\r\n&nbsp;setTimeout(function&nbsp;()&nbsp;{\r\n&nbsp;console.log(\"myAsyncFunction&nbsp;done!\");\r\n&nbsp;resolve({&nbsp;\r\n&nbsp;data:&nbsp;\"Hello,World\"&nbsp;\r\n&nbsp;})\r\n&nbsp;},&nbsp;2000);\r\n&nbsp;});\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u7684\u8bdd\uff0c\u5c31\u4f1a\u5982\u4e0b\uff0c\u4f7f\u7528\u4e86then\/catch\u7684\u56de\u8c03\u65b9\u5f0f\u6765\u5904\u7406\u8c03\u7528\u7ed3\u679c\u548c\u5f02\u5e38\u5904\u7406\uff1a<\/p>\n<pre>myAsyncFunc().then(function&nbsp;(result)&nbsp;{\r\n&nbsp;console.log(result.data);&nbsp;\/\/Hello,World\r\n}).catch(function&nbsp;(err)&nbsp;{\r\n&nbsp;\/\/...\r\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u800c\u5982\u679c\u662f\u4f7f\u7528co\u7684\u8bdd\uff0c\u5219\u4f1a\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre>co(function&nbsp;*()&nbsp;{\r\n&nbsp;try&nbsp;{\r\n&nbsp;var&nbsp;result&nbsp;=&nbsp;yield&nbsp;myAsyncFunc();\r\n&nbsp;console.log(result.data);&nbsp;\/\/Hello,World\r\n&nbsp;}&nbsp;catch(e)&nbsp;{\r\n&nbsp;}\r\n\r\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\u7684\u5199\u6cd5\uff0c\u662f\u4e0d\u662f\u5c31\u662f\u6211\u4eec\u975e\u5e38\u719f\u6089\u7684\uff0c\u6bd4\u8f83\u7b26\u5408\u903b\u8f91\u601d\u7ef4\u4e60\u60ef\u7684\u540c\u6b65\u5199\u6cd5\u4e86\uff1f<\/p>\n<p>\u5173\u4e8eco\u7684\u7528\u6cd5\uff0c\u5728\u672c\u6587\u4e2d\u6211\u5c31\u4e0d\u591a\u5c55\u5f00\u4e86\uff0c\u6709\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u81ea\u884c\u641c\u7d22\u76f8\u5173\u8d44\u6599\u3002\u6211\u4eca\u5929\u8981\u8bb2\u7684\u662f\uff0c\u5982\u4f55\u5728\u5c0f\u7a0b\u5e8f\u73af\u5883\u4e0b\u9762\u6210\u529f\u7684\u4f7f\u7528co\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>1. \u5f00\u542f\u8bed\u8a00\u8f6c\u8bd1\u9009\u9879<\/strong><\/span><\/p>\n<p>\u7531\u4e8e\u8981\u4f7f\u7528\u5230ES6\u7684generator\uff0c\u800c\u53c8\u8981\u4e3a\u4e86\u517c\u5bb9\u6027\uff0c\u6211\u4eec\u5fc5\u987b\u5bf9ES6\u7684\u8bed\u6cd5\u8fdb\u884c\u964d\u7ea7\u8f6c\u8bd1\uff0c\u53d8\u6210ES5\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/001\/506\/33f2af5e41c426bfd4deb8981ca17e26-0.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5229\u7528co\u5904\u7406\u5f02\u6b65\u6d41\u7a0b\u7684\u5b9e\u4f8b\u6559\u7a0b\u63d2\u56fe\" alt=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5229\u7528co\u5904\u7406\u5f02\u6b65\u6d41\u7a0b\u7684\u5b9e\u4f8b\u6559\u7a0b\u63d2\u56fe\" \/><br \/>\u5f00\u542f\u8bed\u8a00\u8f6c\u8bd1\u9009\u9879<\/p>\n<p><span style=\"color: #ff0000\"><strong>2. \u5f15\u5165generator\u652f\u6491\u5e93<\/strong><\/span><\/p>\n<p>\u7ecf\u8fc7\u8f6c\u8bd1\u540e\u7684\u4ee3\u7801\uff0c\u9700\u8981\u4f9d\u8d56\u4e00\u4e2aregeneratorRuntime\uff0c\u624d\u80fd\u652f\u6301generator\u7279\u6027\u3002Facebook\u5f00\u6e90\u7684\u4e00\u4e2aregenerator\u5c31\u662f\u8fd9\u6837\u4e00\u4e2a\u5e93\uff0cGithub\u5730\u5740\uff1agithub.com\/facebook\/regenerator\/<\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7npm\u6765\u4e0b\u8f7d\u8fd9\u4e2aregenerator\u5e93\uff1a<\/p>\n<pre>npm&nbsp;install&nbsp;regenerator<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5c06\u4e0b\u8f7d\u6587\u4ef6\u4e2d\u540d\u4e3aregenerator-runtime.js\u7684\u6587\u4ef6\u62ff\u51fa\u6765\uff0c\u653e\u5230\u6211\u4eec\u7684\u5c0f\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u53bb\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>3.\u4e0b\u8f7dco\u548cPromise\u5e93<\/strong><\/span><\/p>\n<p>\u63a5\u7740\uff0c\u901a\u8fc7npm\u4e0b\u8f7dco\u5e93\uff1a<\/p>\n<pre>npm&nbsp;install&nbsp;co<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5c06\u4e0b\u8f7d\u6587\u4ef6\u4e2d\u540d\u4e3aco.js\u6587\u4ef6\u62ff\u51fa\u6765\uff0c\u653e\u5230\u6211\u4eec\u7684\u5c0f\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u53bb\u3002<\/p>\n<p>\u56e0\u4e3a\u9700\u8981\u4f9d\u8d56Promise\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u5f15\u5165\u4e00\u4e2aPromise\u5b9e\u73b0\u5e93\u3002\u5728\u6b64\u6211\u4eec\u9009\u7528\u4e00\u4e2a\u5c0f\u800c\u517c\u5bb9\u6027\u597d\u7684\u5e93es6-promise\u3002\u540c\u6837\u53ef\u4ee5\u901a\u8fc7npm\u4e0b\u8f7d\uff1a<\/p>\n<pre>npm&nbsp;install&nbsp;es6-promise<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5c06es6-promise.js\u653e\u5230\u6211\u4eec\u7684\u5c0f\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>4.\u5f15\u5165\u5c0f\u7a0b\u5e8f\u4ee3\u7801\u4e2d<\/strong><\/span><\/p>\n<p>\u4f7f\u7528\u524d\uff0c\u5c06\u8fd9\u4e9b\u5e93\u6b63\u786e\u7684\u5f15\u5165\u6211\u4eec\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>const&nbsp;Promise&nbsp;=&nbsp;global.Promise&nbsp;=&nbsp;require('..\/..\/libs\/es6-promise')\r\nconst&nbsp;regeneratorRuntime&nbsp;=&nbsp;global.regeneratorRuntime&nbsp;=&nbsp;require('..\/..\/libs\/regenerator-runtime')\r\nconst&nbsp;co&nbsp;=&nbsp;require('..\/..\/libs\/co')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u597d\u4e86\uff0c\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u5f00\u59cb\u6109\u5feb\u7684\u4f7f\u7528co\u8fdb\u884c\u7f16\u7a0b\u4e86\u3002<\/p>\n<p>\u3010\u76f8\u5173\u63a8\u8350\u3011<\/p>\n<p>1.&nbsp;<\/p>\n<p>2.&nbsp;<\/p>\n<p>3.&nbsp;<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u5229\u7528co\u5904\u7406\u5f02\u6b65\u6d41\u7a0b\u7684\u5b9e\u4f8b\u6559\u7a0b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u5b66\u4e60\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\uff0c\u4e0b\u9762\u5c31\u5b66\u4e60\u7684\u5185\u5bb9\u8fdb\u884c\u603b\u7ed3\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5229\u7528co\u5904\u7406\u5f02\u6b65\u6d41\u7a0b\u7684\u65b9\u6cd5\uff0c\u6587\u4e2d\u7ed9\u51fa\u4e86\u8be6\u7ec6\u7684\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\u4f9b\u5927\u5bb6\u53c2\u8003\u5b66\u4e60\uff0c\u9700\u8981\u7684\u670b\u53cb\u4eec\u4e0b\u9762\u6765\u4e00\u8d77\u770b\u770b\u5427\u3002 \u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u7684\u662f\u5173\u4e8e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5229\u7528co\u5904\u7406\u5f02\u6b65\u6d41\u7a0b\u7684\u65b9\u6cd5\u6559\u7a0b\uff0c\u5206\u4eab\u51fa\u6765\u4f9b\u5927\u5bb6\u53c2\u8003\u5b66\u4e60\uff0c\u9700\u8981\u7684\u670b\u53cb\u4eec\u4e0b\u9762\u6765\u770b\u770b\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a co co\u662f\u4e00\u4e2a\u57fa\u4e8eES6 Generator\u7279\u6027\u5b9e\u73b0\u7684\u3010\u5f02\u6b65\u6d41\u7a0b\u540c\u6b65\u5316\u3011\u5199\u6cd5\u7684\u5de5\u5177\u5e93\u3002 co\u9700\u8981\u4f7f\u7528Promise\u7279\u6027\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u5148\u6765\u521b\u5efa\u4e00\u4e2a\u4f7f\u7528Promise\u6765\u5904\u7406\u7684\u5f02\u6b65\u65b9\u6cd5\uff1a function&nbsp;myAsyncFunc()&nbsp;{ &nbsp;return&nbsp;new&nbsp;Promise(function&nbsp;(resolve,&nbsp;reject)&nbsp;{ &nbsp;setTimeout(function&nbsp;()&nbsp;{ &nbsp;console.log(&#8220;myAsyncFunction&nbsp;done!&#8221;); &nbsp;resolve({&nbsp; &nbsp;data:&nbsp;&#8220;Hello,World&#8221;&nbsp; &nbsp;}) &nbsp;},&nbsp;2000); &nbsp;}); } \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u7684\u8bdd\uff0c\u5c31\u4f1a\u5982\u4e0b\uff0c\u4f7f\u7528\u4e86then\/catch\u7684\u56de\u8c03\u65b9\u5f0f\u6765\u5904\u7406\u8c03\u7528\u7ed3\u679c\u548c\u5f02\u5e38\u5904\u7406\uff1a myAsyncFunc().then(function&nbsp;(result)&nbsp;{ &nbsp;console.log(result.data);&nbsp;\/\/Hello,World }).catch(function&nbsp;(err)&nbsp;{ &nbsp;\/\/&#8230; }); \u767b\u5f55\u540e\u590d\u5236 \u800c\u5982\u679c\u662f\u4f7f\u7528co\u7684\u8bdd\uff0c\u5219\u4f1a\u50cf\u8fd9\u6837\uff1a co(function&nbsp;*()&nbsp;{ &nbsp;try&nbsp;{ &nbsp;var&nbsp;result&nbsp;=&nbsp;yield&nbsp;myAsyncFunc(); &nbsp;console.log(result.data);&nbsp;\/\/Hello,World &nbsp;}&nbsp;catch(e)&nbsp;{ &nbsp;} }); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\u7684\u5199\u6cd5\uff0c\u662f\u4e0d\u662f\u5c31\u662f\u6211\u4eec\u975e\u5e38\u719f\u6089\u7684\uff0c\u6bd4\u8f83\u7b26\u5408\u903b\u8f91\u601d\u7ef4\u4e60\u60ef\u7684\u540c\u6b65\u5199\u6cd5\u4e86\uff1f \u5173\u4e8eco\u7684\u7528\u6cd5\uff0c\u5728\u672c\u6587\u4e2d\u6211\u5c31\u4e0d\u591a\u5c55\u5f00\u4e86\uff0c\u6709\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u81ea\u884c\u641c\u7d22\u76f8\u5173\u8d44\u6599\u3002\u6211\u4eca\u5929\u8981\u8bb2\u7684\u662f\uff0c\u5982\u4f55\u5728\u5c0f\u7a0b\u5e8f\u73af\u5883\u4e0b\u9762\u6210\u529f\u7684\u4f7f\u7528co\u3002 1. \u5f00\u542f\u8bed\u8a00\u8f6c\u8bd1\u9009\u9879 \u7531\u4e8e\u8981\u4f7f\u7528\u5230ES6\u7684generator\uff0c\u800c\u53c8\u8981\u4e3a\u4e86\u517c\u5bb9\u6027\uff0c\u6211\u4eec\u5fc5\u987b\u5bf9ES6\u7684\u8bed\u6cd5\u8fdb\u884c\u964d\u7ea7\u8f6c\u8bd1\uff0c\u53d8\u6210ES5\u3002 \u5f00\u542f\u8bed\u8a00\u8f6c\u8bd1\u9009\u9879 2. \u5f15\u5165generator\u652f\u6491\u5e93 \u7ecf\u8fc7\u8f6c\u8bd1\u540e\u7684\u4ee3\u7801\uff0c\u9700\u8981\u4f9d\u8d56\u4e00\u4e2aregeneratorRuntime\uff0c\u624d\u80fd\u652f\u6301generator\u7279\u6027\u3002Facebook\u5f00\u6e90\u7684\u4e00\u4e2aregenerator\u5c31\u662f\u8fd9\u6837\u4e00\u4e2a\u5e93\uff0cGithub\u5730\u5740\uff1agithub.com\/facebook\/regenerator\/ \u4f60\u53ef\u4ee5\u901a\u8fc7npm\u6765\u4e0b\u8f7d\u8fd9\u4e2aregenerator\u5e93\uff1a npm&nbsp;install&nbsp;regenerator \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u5c06\u4e0b\u8f7d\u6587\u4ef6\u4e2d\u540d\u4e3aregenerator-runtime.js\u7684\u6587\u4ef6\u62ff\u51fa\u6765\uff0c\u653e\u5230\u6211\u4eec\u7684\u5c0f\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u53bb\u3002 3.\u4e0b\u8f7dco\u548cPromise\u5e93 \u63a5\u7740\uff0c\u901a\u8fc7npm\u4e0b\u8f7dco\u5e93\uff1a npm&nbsp;install&nbsp;co \u767b\u5f55\u540e\u590d\u5236 \u5c06\u4e0b\u8f7d\u6587\u4ef6\u4e2d\u540d\u4e3aco.js\u6587\u4ef6\u62ff\u51fa\u6765\uff0c\u653e\u5230\u6211\u4eec\u7684\u5c0f\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u53bb\u3002 \u56e0\u4e3a\u9700\u8981\u4f9d\u8d56Promise\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u5f15\u5165\u4e00\u4e2aPromise\u5b9e\u73b0\u5e93\u3002\u5728\u6b64\u6211\u4eec\u9009\u7528\u4e00\u4e2a\u5c0f\u800c\u517c\u5bb9\u6027\u597d\u7684\u5e93es6-promise\u3002\u540c\u6837\u53ef\u4ee5\u901a\u8fc7npm\u4e0b\u8f7d\uff1a npm&nbsp;install&nbsp;es6-promise \u767b\u5f55\u540e\u590d\u5236 \u5c06es6-promise.js\u653e\u5230\u6211\u4eec\u7684\u5c0f\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-30296","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=30296"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30296\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=30296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=30296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=30296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}