{"id":31998,"date":"2024-11-25T17:09:20","date_gmt":"2024-11-25T09:09:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31998\/"},"modified":"2024-11-25T17:09:20","modified_gmt":"2024-11-25T09:09:20","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8fpromsie-all%e5%92%8cpromise%e7%9a%84%e9%a1%ba%e5%ba%8f%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31998\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8fpromsie.all\u548cpromise\u7684\u987a\u5e8f\u6267\u884c"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8fpromsie.all\u548cpromise\u987a\u5e8f\u6267\u884c\u7684\u76f8\u5173\u8d44\u6599,\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n<p><strong>\u5fae\u4fe1\u5c0f\u7a0b\u5e8fpromsie.all\u548cpromise\u987a\u5e8f\u6267\u884c<\/strong><\/p>\n<p><strong>\u4e00\u3001\u524d\u8a00<\/strong><\/p>\n<p>\u6700\u8fd1\u5728\u505a\u5c0f\u7a0b\u5e8f\u7684\u5f00\u53d1\uff0c\u78b0\u5230\u7684\u4e00\u4e2a\u9700\u6c42\u5c31\u662f\u8868\u5355\u63d0\u4ea4\uff0c\u63d0\u4ea4\u7684\u8868\u5355\u4e2d\u5305\u542b\u6709\u56fe\u7247\uff0c\u5fae\u4fe1\u8fd9\u8fb9\u7684\u505a\u6cd5\u662f\u5148\u4e0a\u4f20\u56fe\u7247\uff0c\u540e\u53f0\u628a\u56fe\u7247\u540d\u79f0\u548c\u5730\u5740\u8fd4\u56de\u7ed9\u4f60\uff0c\u7136\u540e\u4f60\u628a\u56fe\u7247\u4fe1\u606f\u63d2\u5165\u5230\u8868\u5355\u7684\u76f8\u5e94\u4f4d\u7f6e\u518d\u63d0\u4ea4\u8868\u5355\uff0c\u8fd9\u91cc\u5c31\u6d89\u53ca\u5230\u5982\u4f55\u4e0a\u4f20\u5b8c\u56fe\u7247\u7684\u8bf7\u6c42\u518d\u4e0a\u4f20\u8868\u5355\uff0c\u800c\u4e14\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u91cc\u9762\u5982\u679c\u56fe\u7247\u662f\u591a\u4e2a\u7684\u8bdd\uff0c\u4e5f\u53ea\u80fd\u4e00\u5f20\u5f20\u4e0a\u4f20\u3002\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u4e0a\u4f20\u5b8c\u56fe\u7247\uff08\u591a\u4e2a\u8bf7\u6c42\uff09\uff0c\u62ff\u5230\u8fd4\u56de\u503c\uff0c\u518d\u4e0a\u4f20\u8868\u5355\uff0c\u8be5\u5982\u4f55\u505a\uff1f<\/p>\n<p><strong>\u4e8c\u3001Promise.all\u548cPromise.race<\/strong><\/p>\n<p>\u5148\u6765\u4ecb\u7ecdPromise.all\u548cPromise.race\u65b9\u6cd5\u7684\u4e0d\u540c\u70b9Promise.all(iterable) \u65b9\u6cd5\u6307\u5f53\u6240\u6709\u5728\u53ef\u8fed\u4ee3\u53c2\u6570\u4e2d\u7684 promises \u5df2\u5b8c\u6210\uff0c\u6216\u8005\u7b2c\u4e00\u4e2a\u4f20\u9012\u7684 promise\uff08\u6307 reject\uff09\u5931\u8d25\u65f6\uff0c\u8fd4\u56de promise\u3002iterable\u4e3a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u4f46\u662f\u4e00\u822c\u4e3a\u6570\u7ec4\u3002\u8fd4\u56de\u503c\u4e5f\u662f\u4e00\u4e2aPromise\u5bf9\u8c61\u3002<\/p>\n<p>\u9700\u8981\u660e\u786e\u7684\u51e0\u70b9\uff0cPromise.all\u662f\u5e76\u53d1\u6267\u884c\u7684\u540c\u65f6\u8fd0\u884c\u591a\u4e2aPromise\u5bf9\u8c61\uff0c\u800c\u4e14\u8fd4\u56de\u7684Promise\u5bf9\u8c61\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7684\u5404\u9879\u4e5f\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6267\u884c\u7684\u987a\u5e8f\u8fd4\u56de\u3002<\/p>\n<p>Promise.race(iterable) \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 promise\uff0c\u53c2\u6570iterable\u4e2d\u53ea\u8981\u6709\u4e00\u4e2apromise\u5bf9\u8c61\u201d\u5b8c\u6210\uff08resolve\uff09\u201d\u6216\u201d\u5931\u8d25\uff08reject\uff09\u201d\uff0c\u65b0\u7684promise\u5c31\u4f1a\u7acb\u523b\u201d\u5b8c\u6210\uff08resolve\uff09\u201d\u6216\u8005\u201d\u5931\u8d25\uff08reject\uff09\u201d\uff0c\u5e76\u83b7\u5f97\u4e4b\u524d\u90a3\u4e2apromise\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u6216\u8005\u9519\u8bef\u539f\u56e0\u3002\u6240\u4ee5\u53ea\u8981iterable\u4e2d\u6709\u4e00\u4e2a\u5b8c\u6210\u6216\u8005\u5931\u8d25\u5c31\u7acb\u5373\u8fd4\u56de\u4e00\u4e2apromise\u5bf9\u8c61\u3002\u6839\u636erace\u8fd9\u4e2a\u5355\u8bcd\u4e3a\u8d5b\u8dd1\u4e5f\u80fd\u5f97\u51fa\uff0c\u6700\u5148\u5230\u8fbe\u7684\u7acb\u5373\u8fd4\u56de\u4e00\u4e2apromise\u5bf9\u8c61\u3002<\/p>\n<p>\u6839\u636e\u4e0a\u9762\u7684\u5b9a\u4e49\uff0c\u6211\u4eec\u91c7\u7528\u7684Promise.all\u65b9\u6cd5\u6765\u5b8c\u6210\u6211\u4eec\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<pre>\/\/\u5b58\u50a8promise\u5bf9\u8c61\u7684\u6570\u7ec4\nlet&nbsp;promiseArr&nbsp;=&nbsp;[];\n\/\/\u56fe\u7247\u5730\u5740\u6570\u7ec4\nlet&nbsp;imageList&nbsp;=&nbsp;[];\n\/\/\u5c06\u56fe\u7247\u5730\u5740\u7684\u4e0a\u4f20\u7684promise\u5bf9\u8c61\u52a0\u5165\u5230promiseArr\nfor&nbsp;(let&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u5fae\u4fe1\u56fe\u7247\u4e0a\u4f20\n&nbsp;&nbsp;&nbsp;&nbsp;wx.uploadFile({\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;'https:\/\/xxx.xxx.xxx\/api\/uploadImage',\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filePath:&nbsp;imageList[i],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:&nbsp;'file',\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success:&nbsp;function(res)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u53ef\u4ee5\u5bf9res\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540eresolve\u8fd4\u56de\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resolve(res);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail:&nbsp;function&nbsp;(error)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reject(error);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;complete:&nbsp;function&nbsp;(res)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;})\n&nbsp;&nbsp;});\n&nbsp;&nbsp;promiseArr.push(promise)\n}\n\/\/Promise.all\u5904\u7406promiseArr\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2apromise\u5bf9\u8c61\nPromise.all(promiseArr).then((result)&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;\/\/\u5bf9\u8fd4\u56de\u7684result\u6570\u7ec4\u8fdb\u884c\u5904\u7406\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e09\u3001\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u5728\u505a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u56fe\u7247\u4e0a\u4f20\u529f\u80fd\uff0c\u8fd9\u8fb9\u53ea\u80fd\u5148\u4e0a\u4f20\u56fe\u7247\uff0c\u7136\u540e\u5c06\u56fe\u7247\u540d\u548c\u5730\u5740\u4ee5response\u8fd4\u56de\u3002<\/p>\n<p>\u8fd9\u91cc\u9762\u6211\u4eec\u5c31\u662f\u7528\u4e86promise.all\u65b9\u6cd5\u4f46\u662f\u6709\u4e00\u4e2a\u95ee\u9898\u5c31\u662f\uff0cpromise.all\u662f\u5e76\u53d1\u6267\u884c\u7684\uff0c\u4f46\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e00\u6b21\u53ea\u80fd\u5e76\u53d110\u4e2a\u8bf7\u6c42\u3002<\/p>\n<p>\u5bf9\u4e8e\u56fe\u7247\u4e0a\u4f20\uff0c\u53ef\u80fd\u9700\u8981\u4e00\u6b21\u4e0a\u4f20\u8d85\u8fc710\u5f20\u56fe\u7247\uff0c\u4e5f\u5c31\u662f\u4e00\u6b21\u5e76\u53d1\u8d85\u8fc710\u4e2a\u8bf7\u6c42\uff0c\u8fd9\u6837\u7684\u8bdd\u5fae\u4fe1\u5c31\u4f1a\u62a5\u9519<\/p>\n<p><span style=\"color: #ff0000\">\u201cWAService.js:4 uploadFile:fail createUploadTask:fail exceed max upload connection count 10\u201d\u3002<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/009\/597fa1679012a40f97ecc5028479fa28-0.png\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8fpromsie.all\u548cpromise\u7684\u987a\u5e8f\u6267\u884c\u63d2\u56fe\" alt=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8fpromsie.all\u548cpromise\u7684\u987a\u5e8f\u6267\u884c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u56db\u3001\u987a\u5e8fPromise\u6267\u884c\u5904\u7406<br \/><\/strong><\/p>\n<p>\u56e0\u4e3aPromise.all\u662f\u540c\u65f6\u8fd0\u884c\u591a\u4e2apromsie\u5bf9\u8c61\uff0c\u6240\u4ee5\u5bf9\u4e8e\u8fd9\u79cd\u5e76\u53d1\u7684\u6570\u91cf\uff0c\u5c0f\u7a0b\u5e8f\u662f\u6709\u9650\u5236\u7684\uff0c\u4e00\u6b21\u53ea\u80fd\u5e76\u53d110\u4e2a\uff0c\u6240\u4ee5\u5982\u679c\u60f3\u7a81\u7834\u8fd9\u79cd\u9650\u5236\uff0c\u53ef\u4ee5\u8fdb\u884c\u987a\u5e8f\u6267\u884c\u6bcf\u4e2aPromise\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<pre>\/\/\u987a\u5e8f\u5904\u7406\u51fd\u6570\nfunction&nbsp;sequenceTasks(tasks)&nbsp;{\n&nbsp;&nbsp;\/\/\u8bb0\u5f55\u8fd4\u56de\u503c\n&nbsp;&nbsp;function&nbsp;recordValue(results,&nbsp;value)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;results.push(value);\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;results;\n&nbsp;&nbsp;}\n&nbsp;&nbsp;let&nbsp;pushValue&nbsp;=&nbsp;recordValue.bind(null,&nbsp;[]);\n&nbsp;&nbsp;let&nbsp;promise&nbsp;=&nbsp;Promise.resolve();\n&nbsp;&nbsp;\/\/&nbsp;\u5904\u7406tasks\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u51fd\u6570\u5bf9\u8c61\n&nbsp;&nbsp;for&nbsp;(let&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u5fae\u4fe1\u56fe\u7247\u4e0a\u4f20\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wx.uploadFile({\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;'https:\/\/xxx.xxx.xxx\/api\/uploadImage',\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filePath:&nbsp;imageList[i],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:&nbsp;'file',\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success:&nbsp;function(res)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u53ef\u4ee5\u5bf9res\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540eresolve\u8fd4\u56de\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resolve(res);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail:&nbsp;function&nbsp;(error)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reject(error);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;complete:&nbsp;function&nbsp;(res)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})\n&nbsp;&nbsp;&nbsp;&nbsp;});\n&nbsp;&nbsp;};\n&nbsp;&nbsp;promiseFuncArr.push(promiseTemp)\n}\n\nsequenceTasks(promiseFuncArr).then((result)&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;\/\/\u5bf9\u8fd4\u56de\u7684result\u6570\u7ec4\u8fdb\u884c\u5904\u7406\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>1.\u8fd9\u91cc\u89e3\u91ca\u4e00\u4e0bsequenceTasks\u51fd\u6570\u7684\u4f5c\u7528<\/p>\n<p>\u9996\u5148recordValue\u51fd\u6570\u4f20\u5165\u4e24\u4e2a\u503c\uff0c\u4e00\u4e2a\u662fresults\u662f\u8fd4\u56de\u7684\u6570\u7ec4\uff0c\u53e6\u4e00\u4e2a\u662fvalue\uff0cvalue\u662f\u4f20\u5165\u7684\u503c\uff0cresults.push(value);\u5c06\u6bcf\u4e00\u4e2a\u503cpush\u5230results\u6570\u7ec4\uff0c\u7136\u540e\u518d\u8fd4\u56deresults\u6570\u7ec4\u3002<\/p>\n<\/p>\n<pre>let&nbsp;pushValue&nbsp;=&nbsp;recordValue.bind(null,&nbsp;[]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>pushValue\u4e5f\u662f\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u5c06recordValue bind\u5230\u4e00\u4e2a[ ]\u6570\u7ec4\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20null\u4ee3\u8868\uff0c\u4e0d\u6539\u53d8\u51fd\u6570this\u7684\u6307\u5411\uff0c\u6240\u4ee5pushValue\u5f97\u5230\u5c31\u662f\u4e00\u4e2afunction (value)\u7684\u51fd\u6570\uff0c\u53c2\u6570\u4f20\u5165value\u3002<\/p>\n<\/p>\n<pre>promise&nbsp;=&nbsp;promise.then(task).then(pushValue);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>task\u662f\u51fd\u6570\uff0c\u51fd\u6570\u8fd4\u56depromise\u5bf9\u8c61\uff0c\u5728\u6211\u4eec\u8fd9\u91cc\u5c31\u662f\u4e0a\u4f20\u56fe\u7247\u51fd\u6570\uff0c\u6bcf\u4e00\u5f20\u56fe\u7247\u4e0a\u4f20\u90fd\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0cthen(pushValue)\uff0cpushValue\u662ffunction (value)\u7684\u51fd\u6570\uff0cvalue\u4ee3\u8868\u7684\u5c31\u662f\u56fe\u7247\u4e0a\u4f20\u4e4b\u540e\u7684\u8fd4\u56de\u503c\uff0cpushValue\u5c06\u8fd4\u56de\u503cpush\u5230result\u6570\u7ec4\u4e2d\uff0c\u4f9d\u6b21\u6267\u884c\uff0c\u4f9d\u6b21\u52a0\u5165\u5230result\u6570\u7ec4\u4e2d\uff0c\u6700\u540e\u8fd4\u56de\u3002\u5c31\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u5c31\u662f\u4f9d\u6b21\u6267\u884c\u8fd4\u56de\u7684\u7ed3\u679c\u3002<\/p>\n<p>2. sequenceTasks\u4e5f\u91cc\u9762\u7684for\u5faa\u73af\uff0c\u4e5f\u53ef\u4ee5\u5199\u6210\u5982\u4e0b\u7684reduce\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<pre>function&nbsp;sequenceTasks(tasks)&nbsp;{\n&nbsp;&nbsp;\/\/\u8bb0\u5f55\u8fd4\u56de\u503c\n&nbsp;&nbsp;function&nbsp;recordValue(results,&nbsp;value)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;results.push(value);\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;results;\n&nbsp;&nbsp;}\n&nbsp;&nbsp;let&nbsp;pushValue&nbsp;=&nbsp;recordValue.bind(null,&nbsp;[]);\n&nbsp;&nbsp;return&nbsp;tasks.reduce(function&nbsp;(promise,&nbsp;task)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;promise.then(task).then(pushValue);\n&nbsp;&nbsp;},&nbsp;Promise.resolve());\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff0c\u66f4\u591a\u76f8\u5173\u5185\u5bb9\u8bf7\u5173\u6ce8PHP\u4e2d\u6587\u7f51\uff01<\/p>\n<p>\u76f8\u5173\u63a8\u8350\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<\/p>\n<p><span><\/span><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8fpromsie.all\u548cpromise\u7684\u987a\u5e8f\u6267\u884c\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\u5e8fpromsie.all\u548cpromise\u987a\u5e8f\u6267\u884c\u7684\u76f8\u5173\u8d44\u6599,\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b \u5fae\u4fe1\u5c0f\u7a0b\u5e8fpromsie.all\u548cpromise\u987a\u5e8f\u6267\u884c \u4e00\u3001\u524d\u8a00 \u6700\u8fd1\u5728\u505a\u5c0f\u7a0b\u5e8f\u7684\u5f00\u53d1\uff0c\u78b0\u5230\u7684\u4e00\u4e2a\u9700\u6c42\u5c31\u662f\u8868\u5355\u63d0\u4ea4\uff0c\u63d0\u4ea4\u7684\u8868\u5355\u4e2d\u5305\u542b\u6709\u56fe\u7247\uff0c\u5fae\u4fe1\u8fd9\u8fb9\u7684\u505a\u6cd5\u662f\u5148\u4e0a\u4f20\u56fe\u7247\uff0c\u540e\u53f0\u628a\u56fe\u7247\u540d\u79f0\u548c\u5730\u5740\u8fd4\u56de\u7ed9\u4f60\uff0c\u7136\u540e\u4f60\u628a\u56fe\u7247\u4fe1\u606f\u63d2\u5165\u5230\u8868\u5355\u7684\u76f8\u5e94\u4f4d\u7f6e\u518d\u63d0\u4ea4\u8868\u5355\uff0c\u8fd9\u91cc\u5c31\u6d89\u53ca\u5230\u5982\u4f55\u4e0a\u4f20\u5b8c\u56fe\u7247\u7684\u8bf7\u6c42\u518d\u4e0a\u4f20\u8868\u5355\uff0c\u800c\u4e14\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u91cc\u9762\u5982\u679c\u56fe\u7247\u662f\u591a\u4e2a\u7684\u8bdd\uff0c\u4e5f\u53ea\u80fd\u4e00\u5f20\u5f20\u4e0a\u4f20\u3002\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u4e0a\u4f20\u5b8c\u56fe\u7247\uff08\u591a\u4e2a\u8bf7\u6c42\uff09\uff0c\u62ff\u5230\u8fd4\u56de\u503c\uff0c\u518d\u4e0a\u4f20\u8868\u5355\uff0c\u8be5\u5982\u4f55\u505a\uff1f \u4e8c\u3001Promise.all\u548cPromise.race \u5148\u6765\u4ecb\u7ecdPromise.all\u548cPromise.race\u65b9\u6cd5\u7684\u4e0d\u540c\u70b9Promise.all(iterable) \u65b9\u6cd5\u6307\u5f53\u6240\u6709\u5728\u53ef\u8fed\u4ee3\u53c2\u6570\u4e2d\u7684 promises \u5df2\u5b8c\u6210\uff0c\u6216\u8005\u7b2c\u4e00\u4e2a\u4f20\u9012\u7684 promise\uff08\u6307 reject\uff09\u5931\u8d25\u65f6\uff0c\u8fd4\u56de promise\u3002iterable\u4e3a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u4f46\u662f\u4e00\u822c\u4e3a\u6570\u7ec4\u3002\u8fd4\u56de\u503c\u4e5f\u662f\u4e00\u4e2aPromise\u5bf9\u8c61\u3002 \u9700\u8981\u660e\u786e\u7684\u51e0\u70b9\uff0cPromise.all\u662f\u5e76\u53d1\u6267\u884c\u7684\u540c\u65f6\u8fd0\u884c\u591a\u4e2aPromise\u5bf9\u8c61\uff0c\u800c\u4e14\u8fd4\u56de\u7684Promise\u5bf9\u8c61\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7684\u5404\u9879\u4e5f\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6267\u884c\u7684\u987a\u5e8f\u8fd4\u56de\u3002 Promise.race(iterable) \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 promise\uff0c\u53c2\u6570iterable\u4e2d\u53ea\u8981\u6709\u4e00\u4e2apromise\u5bf9\u8c61\u201d\u5b8c\u6210\uff08resolve\uff09\u201d\u6216\u201d\u5931\u8d25\uff08reject\uff09\u201d\uff0c\u65b0\u7684promise\u5c31\u4f1a\u7acb\u523b\u201d\u5b8c\u6210\uff08resolve\uff09\u201d\u6216\u8005\u201d\u5931\u8d25\uff08reject\uff09\u201d\uff0c\u5e76\u83b7\u5f97\u4e4b\u524d\u90a3\u4e2apromise\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u6216\u8005\u9519\u8bef\u539f\u56e0\u3002\u6240\u4ee5\u53ea\u8981iterable\u4e2d\u6709\u4e00\u4e2a\u5b8c\u6210\u6216\u8005\u5931\u8d25\u5c31\u7acb\u5373\u8fd4\u56de\u4e00\u4e2apromise\u5bf9\u8c61\u3002\u6839\u636erace\u8fd9\u4e2a\u5355\u8bcd\u4e3a\u8d5b\u8dd1\u4e5f\u80fd\u5f97\u51fa\uff0c\u6700\u5148\u5230\u8fbe\u7684\u7acb\u5373\u8fd4\u56de\u4e00\u4e2apromise\u5bf9\u8c61\u3002 \u6839\u636e\u4e0a\u9762\u7684\u5b9a\u4e49\uff0c\u6211\u4eec\u91c7\u7528\u7684Promise.all\u65b9\u6cd5\u6765\u5b8c\u6210\u6211\u4eec\u7684\u9700\u6c42\u3002 \/\/\u5b58\u50a8promise\u5bf9\u8c61\u7684\u6570\u7ec4 let&nbsp;promiseArr&nbsp;=&nbsp;[]; \/\/\u56fe\u7247\u5730\u5740\u6570\u7ec4 let&nbsp;imageList&nbsp;=&nbsp;[]; \/\/\u5c06\u56fe\u7247\u5730\u5740\u7684\u4e0a\u4f20\u7684promise\u5bf9\u8c61\u52a0\u5165\u5230promiseArr for&nbsp;(let&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;\/\/\u5fae\u4fe1\u56fe\u7247\u4e0a\u4f20 &nbsp;&nbsp;&nbsp;&nbsp;wx.uploadFile({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;&#8216;https:\/\/xxx.xxx.xxx\/api\/uploadImage&#8217;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filePath:&nbsp;imageList[i], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name:&nbsp;&#8216;file&#8217;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success:&nbsp;function(res)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u53ef\u4ee5\u5bf9res\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540eresolve\u8fd4\u56de &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resolve(res); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail:&nbsp;function&nbsp;(error)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reject(error); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;complete:&nbsp;function&nbsp;(res)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;}); &nbsp;&nbsp;promiseArr.push(promise) } \/\/Promise.all\u5904\u7406promiseArr\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2apromise\u5bf9\u8c61 Promise.all(promiseArr).then((result)&nbsp;=&amp;gt;&nbsp;{ &nbsp;&nbsp;\/\/\u5bf9\u8fd4\u56de\u7684result\u6570\u7ec4\u8fdb\u884c\u5904\u7406 }) \u767b\u5f55\u540e\u590d\u5236 \u4e09\u3001\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u95ee\u9898 \u5728\u505a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u56fe\u7247\u4e0a\u4f20\u529f\u80fd\uff0c\u8fd9\u8fb9\u53ea\u80fd\u5148\u4e0a\u4f20\u56fe\u7247\uff0c\u7136\u540e\u5c06\u56fe\u7247\u540d\u548c\u5730\u5740\u4ee5response\u8fd4\u56de\u3002 \u8fd9\u91cc\u9762\u6211\u4eec\u5c31\u662f\u7528\u4e86promise.all\u65b9\u6cd5\u4f46\u662f\u6709\u4e00\u4e2a\u95ee\u9898\u5c31\u662f\uff0cpromise.all\u662f\u5e76\u53d1\u6267\u884c\u7684\uff0c\u4f46\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e00\u6b21\u53ea\u80fd\u5e76\u53d110\u4e2a\u8bf7\u6c42\u3002 \u5bf9\u4e8e\u56fe\u7247\u4e0a\u4f20\uff0c\u53ef\u80fd\u9700\u8981\u4e00\u6b21\u4e0a\u4f20\u8d85\u8fc710\u5f20\u56fe\u7247\uff0c\u4e5f\u5c31\u662f\u4e00\u6b21\u5e76\u53d1\u8d85\u8fc710\u4e2a\u8bf7\u6c42\uff0c\u8fd9\u6837\u7684\u8bdd\u5fae\u4fe1\u5c31\u4f1a\u62a5\u9519 \u201cWAService.js:4 uploadFile:fail createUploadTask:fail exceed max [&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-31998","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31998","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=31998"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31998\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}