{"id":30878,"date":"2024-11-25T08:46:56","date_gmt":"2024-11-25T00:46:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/30878\/"},"modified":"2024-11-25T08:46:56","modified_gmt":"2024-11-25T00:46:56","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%b8%adpromise%e8%bf%9b%e8%a1%8c%e5%bc%82%e6%ad%a5%e6%b5%81%e7%a8%8b%e5%a4%84%e7%90%86%e7%9a%84%e5%ae%9e%e7%8e%b0%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/30878\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2dPromise\u8fdb\u884c\u5f02\u6b65\u6d41\u7a0b\u5904\u7406\u7684\u5b9e\u73b0\u8fc7\u7a0b"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528promise\u8fdb\u884c\u5f02\u6b65\u6d41\u7a0b\u5904\u7406\u7684\u5b9e\u4f8b\u8be6\u89e3\u7684\u76f8\u5173\u8d44\u6599,\u8fd9\u91cc\u8be6\u7ec6\u8bf4\u660e\u8be5\u5982\u4f55\u4f7f\u7528promise \u6765\u8fdb\u884c\u5f02\u6b65\u6d41\u7a0b\u7684\u5904\u7406\uff0c\u63d0\u4f9b\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n<p><strong>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528Promise\u8fdb\u884c\u5f02\u6b65\u6d41\u7a0b\u5904\u7406\u7684\u5b9e\u4f8b\u8be6\u89e3<\/strong><\/p>\n<p>\u6211\u4eec\u77e5\u9053\uff0cJavaScript\u662f\u5355\u8fdb\u7a0b\u6267\u884c\u7684\uff0c\u540c\u6b65\u64cd\u4f5c\u4f1a\u5bf9\u7a0b\u5e8f\u7684\u6267\u884c\u8fdb\u884c\u963b\u585e\u5904\u7406\u3002\u6bd4\u5982\u5728\u6d4f\u89c8\u5668\u9875\u9762\u7a0b\u5e8f\u4e2d\uff0c\u5982\u679c\u4e00\u6bb5\u540c\u6b65\u7684\u4ee3\u7801\u9700\u8981\u6267\u884c\u5f88\u957f\u65f6\u95f4\uff08\u6bd4\u5982\u4e00\u4e2a\u5f88\u5927\u7684\u5faa\u73af\u64cd\u4f5c\uff09\uff0c\u5219\u9875\u9762\u4f1a\u4ea7\u751f\u5361\u6b7b\u7684\u73b0\u8c61\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u5728JavaScript\u4e2d\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5f02\u6b65\u7279\u6027\uff0c\u4e3a\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u6027\u80fd\u548c\u4f53\u9a8c\u4e0a\u7684\u76ca\u5904\uff0c\u6bd4\u5982\u53ef\u4ee5\u5c06\u4ee3\u7801\u653e\u5230setTimeout()\u4e2d\u6267\u884c\uff1b\u6216\u8005\u5728\u7f51\u9875\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Ajax\u7684\u65b9\u5f0f\u5411\u670d\u52a1\u5668\u7aef\u505a\u5f02\u6b65\u6570\u636e\u8bf7\u6c42\u3002\u8fd9\u4e9b\u5f02\u6b65\u7684\u4ee3\u7801\u4e0d\u4f1a\u963b\u585e\u5f53\u524d\u7684\u754c\u9762\u4e3b\u8fdb\u7a0b\uff0c\u754c\u9762\u8fd8\u662f\u53ef\u4ee5\u7075\u6d3b\u7684\u8fdb\u884c\u64cd\u4f5c\uff0c\u7b49\u5230\u5f02\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\u6210\uff0c\u518d\u505a\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<p>\u4e00\u6bb5\u5178\u578b\u7684\u5f02\u6b65\u4ee3\u7801\u7c7b\u4f3c\u8fd9\u6837\uff1a<\/p>\n<pre>function&nbsp;asyncFunc(callback)&nbsp;{\r\n&nbsp;setTimeout(function&nbsp;()&nbsp;{\r\n&nbsp;&nbsp;\/\/\u5728\u8fd9\u91cc\u5199\u4f60\u7684\u903b\u8f91\u4ee3\u7801\r\n&nbsp;&nbsp;\/\/...\r\n\r\n&nbsp;&nbsp;\/\/\u903b\u8f91\u4ee3\u7801\u7ed3\u675f\uff0c\u6267\u884c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\r\n&nbsp;&nbsp;callback();\r\n&nbsp;},&nbsp;5000);\r\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6216\u8005\uff1a<\/p>\n<pre>function&nbsp;getAccountInfo(callback,&nbsp;errorCallback)&nbsp;{\r\n&nbsp;wx.request({\r\n&nbsp;&nbsp;url:&nbsp;'\/accounts\/12345',\r\n&nbsp;&nbsp;success:&nbsp;function&nbsp;(res)&nbsp;{\r\n&nbsp;&nbsp;&nbsp;\/\/...\r\n&nbsp;&nbsp;&nbsp;callback(data);\r\n&nbsp;&nbsp;},\r\n&nbsp;&nbsp;fail:&nbsp;function&nbsp;(res)&nbsp;{\r\n&nbsp;&nbsp;&nbsp;\/\/...\r\n&nbsp;&nbsp;&nbsp;errorCallback(data);\r\n&nbsp;&nbsp;}\r\n&nbsp;});\r\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\u6211\u4eec\u8fd9\u6837\u8c03\u7528\uff1a<\/p>\n<pre>asyncFunc(function&nbsp;()&nbsp;{\r\n&nbsp;console.log(\"asyncFunc()&nbsp;run&nbsp;complete\");\r\n});\r\n\r\ngetAccountInfo(function&nbsp;(data)&nbsp;{\r\n&nbsp;console.log(\"get&nbsp;account&nbsp;info&nbsp;successfully:\",&nbsp;data);\r\n},&nbsp;function&nbsp;()&nbsp;{\r\n&nbsp;console.error(\"get&nbsp;account&nbsp;info&nbsp;failed\");\r\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u662f\u4e00\u79cd\u4f7f\u7528\u4e86\u56de\u8c03\u51fd\u6570\u6765\u63a7\u5236\u4ee3\u7801\u6267\u884c\u6d41\u7a0b\u7684\u65b9\u5f0f\u3002\u8fd9\u6837\u770b\u8d77\u6765\u6ca1\u95ee\u9898\uff0c\u4e5f\u633a\u5bb9\u6613\u7406\u89e3\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u4e00\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5f02\u6b65\u64cd\u4f5c\u592a\u591a\uff0c\u53c8\u8981\u4fdd\u8bc1\u8fd9\u4e9b\u5f02\u6b65\u64cd\u4f5c\u662f\u6709\u987a\u5e8f\u7684\u6267\u884c\uff0c\u90a3\u6211\u4eec\u7684\u4ee3\u7801\u5c31\u770b\u8d77\u6765\u975e\u5e38\u7cdf\u7cd5\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre>asyncFunc1(function(){\r\n&nbsp;\/\/...\r\n&nbsp;asyncFunc2(function(){\r\n&nbsp;&nbsp;\/\/...\r\n&nbsp;&nbsp;asyncFunc3(function(){\r\n&nbsp;&nbsp;&nbsp;\/\/...\r\n&nbsp;&nbsp;&nbsp;asyncFunc4(function(){\r\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/...\r\n&nbsp;&nbsp;&nbsp;&nbsp;asyncFunc5(function(){\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/...\r\n&nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp;&nbsp;});\r\n&nbsp;&nbsp;});\r\n&nbsp;});\r\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u53ef\u60f3\u800c\u77e5\u4e86\u3002\u8fd8\u6709\uff0c\u56de\u8c03\u51fd\u6570\u771f\u6b63\u7684\u95ee\u9898\u5728\u4e8e\uff1a<\/p>\n<p><span style=\"color: #0000ff\">\u5b83\u5265\u593a\u4e86\u6211\u4eec\u4f7f\u7528 return \u548c throw \u8fd9\u4e9b\u5173\u952e\u5b57\u7684\u80fd\u529b\u3002<\/span><\/p>\n<p>\u90a3\u6709\u4ec0\u4e48\u529e\u6cd5\u6765\u6539\u5584\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0cPromise\u8fd9\u79cd\u6982\u5ff5\u7684\u4ea7\u751f\uff0c\u5f88\u597d\u5730\u89e3\u51b3\u4e86\u8fd9\u4e00\u5207\u3002\u5173\u4e8e\u4ec0\u4e48\u662fPromise\uff0c\u4e00\u641c\u4e00\u5927\u628a\u4ecb\u7ecd\uff0c\u6211\u8fd9\u91cc\u5c31\u4e0d\u590d\u5236\u7c98\u8d34\u4e86\uff0c\u6211\u4e3b\u8981\u662f\u8bb2\u4e00\u4e0b\u6211\u4eec\u600e\u4e48\u7528\u5b83\u6765\u89e3\u51b3\u6211\u4eec\u7684\u95ee\u9898\u3002<\/p>\n<p>\u6211\u4eec\u6765\u770b\u4e00\u4e0b\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u5982\u679c\u4f7f\u7528Promise\uff0c\u5b83\u4f1a\u662f\u4ec0\u4e48\u6837\u5b50?\u6211\u4eec\u5148\u5c06\u8fd9\u4e9b\u51fd\u6570\u53d8\u6210Promise\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre>function&nbsp;asyncFunc1(){\r\n&nbsp;return&nbsp;new&nbsp;Promise(function&nbsp;(resolve,&nbsp;reject)&nbsp;{\r\n&nbsp;&nbsp;\/\/...\r\n&nbsp;})\r\n}\r\n\r\n&nbsp;\r\n\/\/&nbsp;asyncFunc2,3,4,5\u4e5f\u5b9e\u73b0\u6210\u8ddfasyncFunc1\u4e00\u6837\u7684\u65b9\u5f0f...<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\u770b\u4e00\u4e0b\u4ed6\u4eec\u662f\u600e\u4e48\u6837\u88ab\u8c03\u7528\u7684\uff1a<\/p>\n<pre>asyncFunc1()\r\n&nbsp;.then(asyncFunc2)\r\n&nbsp;.then(asyncFunc3)\r\n&nbsp;.then(asyncFunc4)\r\n&nbsp;.then(asyncFunc5);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\uff0c\u8fd9\u4e9b\u5f02\u6b65\u51fd\u6570\u5c31\u4f1a\u6309\u7167\u987a\u5e8f\u4e00\u4e2a\u4e00\u4e2a\u4f9d\u6b21\u6267\u884c\u4e86\u3002<\/p>\n<p>ES6\u4e2d\u539f\u751f\u652f\u6301\u4e86Promise\uff0c\u4e0d\u8fc7\u5728\u539f\u751f\u4e0d\u652f\u6301Promise\u7684\u73af\u5883\u4e2d\uff0c\u6211\u4eec\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\u6765\u652f\u6301\uff0c\u6bd4\u5982Q.js\u548cBluebird\u3002\u5b83\u4eec\u4e00\u822c\u90fd\u9664\u4e86\u63d0\u4f9b\u6807\u51c6Promise\u7684API\u5916\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6807\u51c6\u4e4b\u5916\u4f46\u975e\u5e38\u6709\u7528\u7684API\uff0c\u4f7f\u5f97\u5f02\u6b65\u6d41\u7a0b\u7684\u63a7\u5236\u66f4\u52a0\u4f18\u96c5\u3002<\/p>\n<p>\u4ece\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684API\u6587\u6863\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u6846\u67b6\u63d0\u4f9b\u7684JavaScript API\u4e2d\u5f88\u591a\u51fd\u6570\u5176\u5b9e\u90fd\u662f\u5f02\u6b65\u7684\uff0c\u5982wx.setStorage(), wx.getStorage(), wx.getLocation()\u7b49\u7b49\uff0c\u5b83\u4eec\u4e5f\u662f\u63d0\u4f9b\u7684\u56de\u8c03\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u5728\u53c2\u6570\u4e2d\u4f20\u5165success, fail\uff0ccomplete\u56de\u8c03\u51fd\u6570\uff0c\u5c31\u53ef\u4ee5\u5bf9\u8fd0\u884c\u6210\u529f\u6216\u5931\u8d25\u8fdb\u884c\u5206\u522b\u5904\u7406\u3002<\/p>\n<p>\u5982\uff1a<\/p>\n<pre>wx.getLocation({&nbsp;\r\n&nbsp;type:&nbsp;'wgs84',&nbsp;\r\n&nbsp;success:&nbsp;function(res)&nbsp;{&nbsp;\r\n&nbsp;&nbsp;var&nbsp;latitude&nbsp;=&nbsp;res.latitude&nbsp;\r\n&nbsp;&nbsp;var&nbsp;longitude&nbsp;=&nbsp;res.longitude&nbsp;\r\n&nbsp;&nbsp;var&nbsp;speed&nbsp;=&nbsp;res.speed&nbsp;\r\n&nbsp;&nbsp;var&nbsp;accuracy&nbsp;=&nbsp;res.accuracy&nbsp;\r\n&nbsp;},\r\n&nbsp;fail:&nbsp;function()&nbsp;{\r\n&nbsp;&nbsp;console.error(\"get&nbsp;location&nbsp;failed\")\r\n&nbsp;}\r\n})<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6211\u4eec\u80fd\u4e0d\u80fd\u8ba9\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u5f02\u6b65API\u652f\u6301Promise\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0c\u6211\u4eec\u5f53\u7136\u53ef\u4ee5\u4e00\u4e2a\u4e00\u4e2a\u7684\u7528Promise\u53bb\u5305\u88c5\u8fd9\u4e9bAPI\uff0c\u4f46\u662f\u8fd9\u4e2a\u8fd8\u662f\u6bd4\u8f83\u9ebb\u70e6\u7684\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u5c0f\u7a0b\u5e8f\u7684API\u7684\u53c2\u6570\u683c\u5f0f\u90fd\u6bd4\u8f83\u7edf\u4e00\uff0c\u53ea\u63a5\u53d7\u4e00\u4e2aobject\u53c2\u6570\uff0c\u56de\u8c03\u90fd\u662f\u5728\u8fd9\u4e2a\u53c2\u6570\u4e2d\u8bbe\u7f6e\uff0c\u6240\u4ee5\uff0c\u8fd9\u4e3a\u6211\u4eec\u7684\u7edf\u4e00\u5904\u7406\u63d0\u4f9b\u4e86\u4fbf\u5229\uff0c\u6211\u4eec\u53ef\u4ee5\u5199\u4e00\u4e2a\u975e\u4fb5\u5165\u6027\u7684\u5de5\u5177\u65b9\u6cd5\uff0c\u6765\u5b8c\u6210\u8fd9\u6837\u7684\u5de5\u4f5c\uff1a<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u5c06\u8fd9\u4e2a\u5de5\u5177\u65b9\u6cd5\u5199\u5230\u4e00\u4e2a\u540d\u4e3a\u7684util.js\u7684\u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre>var&nbsp;Promise&nbsp;=&nbsp;require('..\/libs\/bluebird.min')&nbsp;&nbsp;\/\/\u6211\u7528\u4e86bluebird.js\r\nfunction&nbsp;wxPromisify(fn)&nbsp;{&nbsp;\r\n&nbsp;return&nbsp;function&nbsp;(obj&nbsp;=&nbsp;{})&nbsp;{&nbsp;&nbsp;\r\n&nbsp;&nbsp;return&nbsp;new&nbsp;Promise((resolve,&nbsp;reject)&nbsp;=&amp;gt;&nbsp;{&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;obj.success&nbsp;=&nbsp;function&nbsp;(res)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;resolve(res)&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;\r\n\r\n&nbsp;&nbsp;&nbsp;obj.fail&nbsp;=&nbsp;function&nbsp;(res)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;reject(res)&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;\r\n\r\n&nbsp;&nbsp;&nbsp;fn(obj)&nbsp;&nbsp;\r\n&nbsp;&nbsp;})&nbsp;\r\n&nbsp;}\r\n}\r\n\r\nmodule.exports&nbsp;=&nbsp;{&nbsp;\r\n&nbsp;wxPromisify:&nbsp;wxPromisify\r\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e4b\u540e\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u5c06\u539f\u6765\u56de\u8c03\u65b9\u5f0f\u7684API\u53d8\u6210Promise\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre>var&nbsp;util&nbsp;=&nbsp;require('..\/utils\/util')\r\n\r\nvar&nbsp;getLocationPromisified&nbsp;=&nbsp;util.wxPromisify(wx.getLocation)\r\n\r\ngetLocationPromisified({\r\n&nbsp;type:&nbsp;'wgs84'\r\n}).then(function&nbsp;(res)&nbsp;{\r\n&nbsp;var&nbsp;latitude&nbsp;=&nbsp;res.latitude&nbsp;\r\n&nbsp;var&nbsp;longitude&nbsp;=&nbsp;res.longitude&nbsp;\r\n&nbsp;var&nbsp;speed&nbsp;=&nbsp;res.speed&nbsp;\r\n&nbsp;var&nbsp;accuracy&nbsp;=&nbsp;res.accuracy&nbsp;\r\n}).catch(function&nbsp;()&nbsp;{\r\n&nbsp;console.error(\"get&nbsp;location&nbsp;failed\")\r\n})<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u662f\u4e0d\u662f\u5f88\u5bb9\u6613\u7406\u89e3\uff1f<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2dPromise\u8fdb\u884c\u5f02\u6b65\u6d41\u7a0b\u5904\u7406\u7684\u5b9e\u73b0\u8fc7\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>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528promise\u8fdb\u884c\u5f02\u6b65\u6d41\u7a0b\u5904\u7406\u7684\u5b9e\u4f8b\u8be6\u89e3\u7684\u76f8\u5173\u8d44\u6599,\u8fd9\u91cc\u8be6\u7ec6\u8bf4\u660e\u8be5\u5982\u4f55\u4f7f\u7528promise \u6765\u8fdb\u884c\u5f02\u6b65\u6d41\u7a0b\u7684\u5904\u7406\uff0c\u63d0\u4f9b\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b \u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u4f7f\u7528Promise\u8fdb\u884c\u5f02\u6b65\u6d41\u7a0b\u5904\u7406\u7684\u5b9e\u4f8b\u8be6\u89e3 \u6211\u4eec\u77e5\u9053\uff0cJavaScript\u662f\u5355\u8fdb\u7a0b\u6267\u884c\u7684\uff0c\u540c\u6b65\u64cd\u4f5c\u4f1a\u5bf9\u7a0b\u5e8f\u7684\u6267\u884c\u8fdb\u884c\u963b\u585e\u5904\u7406\u3002\u6bd4\u5982\u5728\u6d4f\u89c8\u5668\u9875\u9762\u7a0b\u5e8f\u4e2d\uff0c\u5982\u679c\u4e00\u6bb5\u540c\u6b65\u7684\u4ee3\u7801\u9700\u8981\u6267\u884c\u5f88\u957f\u65f6\u95f4\uff08\u6bd4\u5982\u4e00\u4e2a\u5f88\u5927\u7684\u5faa\u73af\u64cd\u4f5c\uff09\uff0c\u5219\u9875\u9762\u4f1a\u4ea7\u751f\u5361\u6b7b\u7684\u73b0\u8c61\u3002 \u6240\u4ee5\uff0c\u5728JavaScript\u4e2d\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5f02\u6b65\u7279\u6027\uff0c\u4e3a\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u6027\u80fd\u548c\u4f53\u9a8c\u4e0a\u7684\u76ca\u5904\uff0c\u6bd4\u5982\u53ef\u4ee5\u5c06\u4ee3\u7801\u653e\u5230setTimeout()\u4e2d\u6267\u884c\uff1b\u6216\u8005\u5728\u7f51\u9875\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Ajax\u7684\u65b9\u5f0f\u5411\u670d\u52a1\u5668\u7aef\u505a\u5f02\u6b65\u6570\u636e\u8bf7\u6c42\u3002\u8fd9\u4e9b\u5f02\u6b65\u7684\u4ee3\u7801\u4e0d\u4f1a\u963b\u585e\u5f53\u524d\u7684\u754c\u9762\u4e3b\u8fdb\u7a0b\uff0c\u754c\u9762\u8fd8\u662f\u53ef\u4ee5\u7075\u6d3b\u7684\u8fdb\u884c\u64cd\u4f5c\uff0c\u7b49\u5230\u5f02\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\u6210\uff0c\u518d\u505a\u76f8\u5e94\u7684\u5904\u7406\u3002 \u4e00\u6bb5\u5178\u578b\u7684\u5f02\u6b65\u4ee3\u7801\u7c7b\u4f3c\u8fd9\u6837\uff1a function&nbsp;asyncFunc(callback)&nbsp;{ &nbsp;setTimeout(function&nbsp;()&nbsp;{ &nbsp;&nbsp;\/\/\u5728\u8fd9\u91cc\u5199\u4f60\u7684\u903b\u8f91\u4ee3\u7801 &nbsp;&nbsp;\/\/&#8230; &nbsp;&nbsp;\/\/\u903b\u8f91\u4ee3\u7801\u7ed3\u675f\uff0c\u6267\u884c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570 &nbsp;&nbsp;callback(); &nbsp;},&nbsp;5000); } \u767b\u5f55\u540e\u590d\u5236 \u6216\u8005\uff1a function&nbsp;getAccountInfo(callback,&nbsp;errorCallback)&nbsp;{ &nbsp;wx.request({ &nbsp;&nbsp;url:&nbsp;&#8216;\/accounts\/12345&#8217;, &nbsp;&nbsp;success:&nbsp;function&nbsp;(res)&nbsp;{ &nbsp;&nbsp;&nbsp;\/\/&#8230; &nbsp;&nbsp;&nbsp;callback(data); &nbsp;&nbsp;}, &nbsp;&nbsp;fail:&nbsp;function&nbsp;(res)&nbsp;{ &nbsp;&nbsp;&nbsp;\/\/&#8230; &nbsp;&nbsp;&nbsp;errorCallback(data); &nbsp;&nbsp;} &nbsp;}); } \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u6211\u4eec\u8fd9\u6837\u8c03\u7528\uff1a asyncFunc(function&nbsp;()&nbsp;{ &nbsp;console.log(&#8220;asyncFunc()&nbsp;run&nbsp;complete&#8221;); }); getAccountInfo(function&nbsp;(data)&nbsp;{ &nbsp;console.log(&#8220;get&nbsp;account&nbsp;info&nbsp;successfully:&#8221;,&nbsp;data); },&nbsp;function&nbsp;()&nbsp;{ &nbsp;console.error(&#8220;get&nbsp;account&nbsp;info&nbsp;failed&#8221;); }); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u662f\u4e00\u79cd\u4f7f\u7528\u4e86\u56de\u8c03\u51fd\u6570\u6765\u63a7\u5236\u4ee3\u7801\u6267\u884c\u6d41\u7a0b\u7684\u65b9\u5f0f\u3002\u8fd9\u6837\u770b\u8d77\u6765\u6ca1\u95ee\u9898\uff0c\u4e5f\u633a\u5bb9\u6613\u7406\u89e3\u3002 \u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u4e00\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5f02\u6b65\u64cd\u4f5c\u592a\u591a\uff0c\u53c8\u8981\u4fdd\u8bc1\u8fd9\u4e9b\u5f02\u6b65\u64cd\u4f5c\u662f\u6709\u987a\u5e8f\u7684\u6267\u884c\uff0c\u90a3\u6211\u4eec\u7684\u4ee3\u7801\u5c31\u770b\u8d77\u6765\u975e\u5e38\u7cdf\u7cd5\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a asyncFunc1(function(){ &nbsp;\/\/&#8230; &nbsp;asyncFunc2(function(){ &nbsp;&nbsp;\/\/&#8230; &nbsp;&nbsp;asyncFunc3(function(){ &nbsp;&nbsp;&nbsp;\/\/&#8230; &nbsp;&nbsp;&nbsp;asyncFunc4(function(){ &nbsp;&nbsp;&nbsp;&nbsp;\/\/&#8230; &nbsp;&nbsp;&nbsp;&nbsp;asyncFunc5(function(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&#8230; &nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;}); &nbsp;}); [&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-30878","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30878","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=30878"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30878\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=30878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=30878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=30878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}