{"id":9941,"date":"2021-05-09T22:51:29","date_gmt":"2021-05-09T14:51:29","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9941"},"modified":"2021-05-13T16:47:57","modified_gmt":"2021-05-13T08:47:57","slug":"promise-all-race-any","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/05\/promise-all-race-any\/","title":{"rendered":"Promise.all\u3001race\u548cany\u65b9\u6cd5\u90fd\u662f\u4ec0\u4e48\u610f\u601d\uff1f"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9941\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9941<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202105-promise.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/promise-cover.png\" width=\"480\" height=\"312\" alt=\"promise\u5c01\u9762\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u533a\u522b\u6982\u8ff0<\/h3>\n<p>\u5148\u4e00\u53e5\u8bdd\u63cf\u8ff0\u4e0b<code>Promise.all()<\/code>\u3001<code>Promise.race()<\/code>\u548c<code>Promise.any()<\/code>\u7684\u533a\u522b\u3002<\/p>\n<ul>\n<li><code>Promise.all()<\/code>\u4e2d\u7684Promise\u5e8f\u5217\u4f1a\u5168\u90e8\u6267\u884c\u901a\u8fc7\u624d\u8ba4\u4e3a\u662f\u6210\u529f\uff0c\u5426\u5219\u8ba4\u4e3a\u662f\u5931\u8d25\uff1b<\/li>\n<li><code>Promise.race()<\/code>\u4e2d\u7684Promise\u5e8f\u5217\u4e2d\u7b2c\u4e00\u4e2a\u6267\u884c\u5b8c\u6bd5\u7684\u662f\u901a\u8fc7\uff0c\u5219\u8ba4\u4e3a\u6210\u529f\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u6267\u884c\u5b8c\u6bd5\u7684Promise\u662f\u62d2\u7edd\uff0c\u5219\u8ba4\u4e3a\u5931\u8d25\uff1b<\/li>\n<li><code>Promise.any()<\/code>\u4e2d\u7684Promise\u5e8f\u5217\u53ea\u8981\u6709\u4e00\u4e2a\u6267\u884c\u901a\u8fc7\uff0c\u5219\u8ba4\u4e3a\u6210\u529f\uff0c\u5982\u679c\u5168\u90e8\u62d2\u7edd\uff0c\u5219\u8ba4\u4e3a\u5931\u8d25\uff1b<\/li>\n<\/ul>\n<p>\u4e00\u4f8b\u80dc\u5343\u8a00\uff0c\u901a\u8fc7\u5b9e\u4f8b\u80fd\u66f4\u597d\u5730\u8868\u793a\u4e0a\u97623\u79cd\u65b9\u6cd5\u7684\u533a\u522b\u3002<\/p>\n<h3>\u4e8c\u3001\u4e3e\u4f8b\u8bf4\u660e\u5dee\u5f02<\/h3>\n<p>\u65e0\u8bba\u662f\u4e0a\u4f20\u56fe\u7247\u8fd8\u662f\u4e0b\u8f7d\u56fe\u7247\uff0c\u90fd\u662f\u5f02\u6b65\u64cd\u4f5c\uff0c\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e24\u4e2a\u5e38\u89c1\u4ea4\u4e92\u53d8\u6210\u4e86\u4e00\u4e2aPromise\u64cd\u4f5c\uff0c\u4e3a\u4e86\u7b80\u5316\u7406\u89e3\uff0c\u6574\u4e2a\u5f02\u6b65\u8fc7\u7a0b\u6211\u4eec\u5c31\u4f7f\u7528\u5b9a\u65f6\u5668\u4ee3\u66ff\uff0c\u793a\u610f\u5982\u4e0b\uff1a<\/p>\n<pre>\r\nconst upload = function (blob) {\r\n    let time = Math.round(100 + 500 * Math.random());\r\n    return new Promise((resolve, reject) => {\r\n        <span class=\"green\">\/\/ \u662f\u5426\u6267\u884c\u6d4b\u8bd5<\/span>\r\n        console.log(`run ${time}ms`);\r\n        <span class=\"green\">\/\/ \u6210\u529f\u5931\u8d25\u6982\u738750%<\/span>\r\n        if (Math.random() > 0.5) {\r\n            setTimeout(resolve, time, 'promise resolved ' + time + 'ms');\r\n        } else {\r\n            setTimeout(reject, time, 'promise rejected ' + time + 'ms');\r\n        }         \r\n    });\r\n};\r\nconst load = function (url) {\r\n    <span class=\"green\">\/\/ \u540cupload<\/span>\r\n};<\/pre>\n<h4>1. Promise.all()<\/h4>\n<p><code>Promise.all()<\/code>\u91cc\u9762\u6240\u6709\u53ef\u8fed\u4ee3\u7684Promise\u90fd\u901a\u8fc7\u5219\u8ba4\u4e3a\u662f\u6210\u529f\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u62d2\u7edd\uff0c\u5219\u8ba4\u4e3a\u5931\u8d25\u3002<\/p>\n<p>\u6d4b\u8bd5\u5982\u4e0b\uff1a<\/p>\n<pre>(async () => {\r\n    try {\r\n        let result = await Promise.all([upload(0), upload(1), upload(2)]);\r\n        console.log(result);\r\n    } catch (err) {\r\n        console.error(err);\r\n    }\r\n})();<\/pre>\n<p>\u53ea\u67093\u4e2aupload\u65b9\u6cd5\u90fdresolve\u901a\u8fc7\u624d\u4f1a\u8ba4\u4e3a\u6210\u529f\uff0c\u5426\u5219\u8fd4\u56de\u7b2c\u4e00\u4e2a\u51fa\u9519\u7684\u63d0\u793a\u7ed3\u6784\uff0c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-06_172741.png\" alt=\"Promise.all()\u6267\u884c\u7ed3\u679c\" width=\"610\" height=\"202\"><\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0cPromise.all()\u6210\u529f\u7684\u6982\u7387\u662f0.5 * 0.5 * 0.5 = 0.125\uff0c\u4e5f\u5c31\u662f\u6210\u529f\u7387\u662f12.5%\u3002<\/p>\n<h4>2. Promise.race()<\/h4>\n<p><code>Promise.race()<\/code>\u987e\u540d\u601d\u610f\u5c31\u662f\u201c\u8d5b\u8dd1\u201d\uff0c\u54ea\u4e2a\u6267\u884c\u5feb\u5c31\u4f7f\u7528\u54ea\u4e2a\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u6bcf\u6b21\u6267\u884c\uff0c\u65e0\u8bba\u6210\u529f\u8fd8\u662f\u5931\u8d25\uff0c\u5176\u8f93\u51fa\u7684\u4fe1\u606f\u4e2d\u7684\u65f6\u95f4\u4e00\u5b9a\u662f\u5ef6\u65f6\u65f6\u95f4\u6700\u77ed\u7684\u90a3\u4e2a\u3002\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>(async () => {\r\n    try {\r\n        let result = await Promise.race([load(0), load(1), load(2)]);\r\n        console.log(result);\r\n    } catch (err) {\r\n        console.error(err);\r\n    }\r\n})();<\/pre>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-06_175949.png\" alt=\"Promise.race\u6267\u884c\u7ed3\u679c\u793a\u610f\" width=\"283\" height=\"278\"><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u65e0\u8bba\u662fresult\u8fd8\u662ferr\u7684\u4fe1\u606f\u4e2d\u7684ms\u65f6\u95f4\uff0c\u5bf9\u5e94\u7684\u5c31\u662f3\u4e2arun\u65f6\u95f4\u4e2d\u6700\u77ed\u7684\u90a3\u4e2a\u3002<\/p>\n<p>\u5e76\u4e14\uff0c\u6210\u529f\u7684\u8fd4\u56de\u503c\u4e0d\u518d\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u800c\u662f\u5355\u4e2a\u63d0\u793a\u3002<\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0cPromise.race()\u6210\u529f\u7684\u6982\u7387\u662f0.5\uff0c\u4e5f\u5c31\u662f\u6210\u529f\u7387\u662f50%\u3002<\/p>\n<h4>3. Promise.any()<\/h4>\n<p><code>Promise.any()<\/code>\u66f4\u5173\u5fc3\u6210\u529f\uff0c\u53ea\u8981\u6709\u4e00\u4e2a\u6210\u529f\u5c31\u53ef\u4ee5\u4e86\uff0c\u9664\u975e\u6240\u6709\u7684Promise\u90fd\u62d2\u7edd\uff0c\u5426\u5219\u5c31\u8ba4\u4e3a\u6210\u529f\u3002<\/p>\n<p>\u7c7b\u4f3c\u7684\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre>(async () => {\r\n    try {\r\n        let result = await Promise.any([load(0), load(1), load(2)]);\r\n        console.log(result);\r\n    } catch (err) {\r\n        console.error(err);\r\n    }\r\n})();<\/pre>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-06_180624.png\" alt=\"Promise.any\u6267\u884c\u7ed3\u679c\u793a\u610f\" width=\"376\" height=\"200\"><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u51fa\u9519\u7684\u7c7b\u578b\u548c\u4e0a\u9762\u4e24\u4e2a\u65b9\u6cd5\u90fd\u4e0d\u4e00\u6837\uff0c\u662fAggregateError\u7c7b\u578b\u7684\u9519\u8bef\u3002<\/p>\n<blockquote><p>AggregateError: All promises were rejected<\/p><\/blockquote>\n<p>\u6210\u529f\u7684\u8fd4\u56de\u503c\u662f\u7b2c\u4e00\u4e2a\u6267\u884c\u6210\u529f\u7684Promise\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0cPromise.any()\u6210\u529f\u7684\u6982\u7387\u662f(1 &#8211; 0.5 * 0.5 * 0.5) = 87.5%\uff0c\u4e5f\u5c31\u662f\u6210\u529f\u7387\u662f87.5%\u3002<\/p>\n<h3>\u4e09\u3001\u4ec0\u4e48\u65f6\u5019\u8be5\u4f7f\u7528\u54ea\u4e2a\uff1f<\/h3>\n<p>\u8fd9\u4e00\u5c0f\u8282\u8bb2\u4e0b\u4e09\u79cdPromise\u65b9\u6cd5\u5408\u9002\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<p>\u5355\u7eaf\u4e3e\u4f8b\uff0c\u8ba9\u5927\u5bb6\u66f4\u597d\u5730\u4e86\u89e3\u8fd93\u79cdPromise\u65b9\u6cd5\u3002<\/p>\n<h4>1. Promise.all()<\/h4>\n<p><code>Promise.all()<\/code>\u5728\u56fe\u7247\u6279\u91cf\u4e0a\u4f20\u7684\u65f6\u5019\u5f88\u6709\u7528\uff0c\u53ef\u4ee5\u77e5\u9053\u4ec0\u4e48\u65f6\u5019\u8fd9\u6279\u56fe\u7247\u5168\u90e8\u4e0a\u4f20\u5b8c\u6bd5\uff0c\u4fdd\u8bc1\u4e86\u5e76\u884c\uff0c\u540c\u65f6\u77e5\u9053\u6700\u7ec8\u7684\u4e0a\u4f20\u7ed3\u679c\u3002<\/p>\n<p>\u53c8\u4f8b\u5982\uff0c\u9875\u9762\u8fdb\u884c\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u5982\u679c\u8bf7\u6c42\u65f6\u95f4\u592a\u77ed\uff0cloading\u56fe\u6807\u5c31\u4f1a\u4e00\u95ea\u800c\u8fc7\uff0c\u4f53\u9a8c\u5e76\u4e0d\u597d\u3002<\/p>\n<p><code>Promise.all()<\/code>\u53ef\u4ee5\u4fdd\u8bc1\u6700\u4f4eloading\u65f6\u95f4\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u4fdd\u8bc1loading\u81f3\u5c11\u51fa\u73b0200ms\uff1a<\/p>\n<pre>let getUserInfo = function (user) {\r\n    return new Promise((resolve, reject) => {\r\n        setTimeout(() => resolve('\u59d3\u540d\uff1a\u5f20\u946b\u65ed'), Math.floor(400 * Math.random()));\r\n    });\r\n}\r\n\r\nlet showUserInfo = function (user) {\r\n    return getUserInfo().then(info => {\r\n        console.log('\u7528\u6237\u4fe1\u606f', info);\r\n        return true;\r\n    });\r\n}\r\n\r\nlet timeout = function (delay, result) {\r\n    return new Promise(resolve => {\r\n        setTimeout(() => resolve(result), delay);\r\n    });\r\n}\r\n<span class=\"green\">\/\/ loading\u65f6\u95f4\u663e\u793a\u9700\u8981<\/span>\r\nconst time = +new Date();\r\nlet showToast = function () {\r\n    console.log('show loading...');\r\n}\r\nlet hideToast = function () {;\r\n    console.log('hide loading' + (+new Date() - time));\r\n}\r\n<span class=\"green\">\/\/ \u6267\u884c\u4ee3\u7801\u793a\u610f<\/span>\r\nshowToast();\r\nPromise.all([showUserInfo(), timeout(200)]).then(() => {\r\n   hideToast();\r\n});<\/pre>\n<p>\u6267\u884c\u6548\u679c\u5982\u4e0b\u6240\u793a\uff0c\u53ef\u4ee5\u770b\u5230loading\u4ece\u663e\u793a\u5230\u9690\u85cf\uff0c\u4e00\u5b9a\u4e0d\u4f1a\u5c0f\u4e8e200ms\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-06_185724.png\" alt=\"loading\u65f6\u95f4\u6267\u884c\u793a\u610f\" width=\"279\" height=\"281\"><\/p>\n<p><span class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4f53\u9a8c\u7cdf\u7cd5\u7684\u5783\u573e\u76d7\u7248\u7f51\u7ad9\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u539f\u6587\u83b7\u5f97\u5f88\u597d\u7684\u4f53\u9a8c\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=9941\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h4>2. Promise.race()<\/h4>\n<p>\u4e0a\u9762\u7684loading\u7b56\u7565\u4ed4\u7ec6\u4e00\u60f3\uff0c\u6709\u4e9b\u602a\u602a\u7684\uff0c\u8bf7\u6c42\u672c\u6765\u5f88\u5feb\uff0c\u8fd8\u975e\u8981\u663e\u793a\u4e00\u4e2aloading\uff0c\u8fd9\u4e0d\u662f\u820d\u672c\u9010\u672b\u4e86\u5417\uff1f<\/p>\n<p>\u6240\u4ee5\u9700\u6c42\u5e94\u8be5\u662f\u8fd9\u6837\uff0c\u5982\u679c\u8bf7\u6c42\u53ef\u4ee5\u5728200ms\u5185\u5b8c\u6210\uff0c\u5219\u4e0d\u663e\u793aloading\uff0c\u5982\u679c\u8981\u8d85\u8fc7200ms\uff0c\u5219\u81f3\u5c11\u663e\u793a200ms\u7684loading\u3002<\/p>\n<p>\u8fd9\u4e2a\u9700\u6c42\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>Promise.race()<\/code>\u65b9\u6cd5\uff0c\u6267\u884c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff08getUserInfo\u3001showUserInfo\u7b49\u65b9\u6cd5\u90fd\u4e0d\u53d8\uff09\u3002<\/p>\n<pre><span class=\"green\">\/\/ loading\u65f6\u95f4\u663e\u793a\u9700\u8981<\/span>\r\nlet time = 0;\r\nlet showToast = function () {\r\n    time = +new Date()\r\n    console.log('show loading...');\r\n}\r\nlet hideToast = function () {;\r\n    console.log('hide loading' + (+new Date() - time));\r\n}\r\n<span class=\"green\">\/\/ \u6267\u884c\u4ee3\u7801\u793a\u610f<\/span>\r\nlet promiseUserInfo = showUserInfo();\r\nPromise.race([promiseUserInfo, timeout(200)]).then((display) => {\r\n    if (!display) {\r\n        showToast();\r\n        \r\n        Promise.all([promiseUserInfo, timeout(200)]).then(() => {\r\n            hideToast();\r\n        });\r\n    }\r\n});<\/pre>\n<p>\u4e8e\u662f\uff0c\u8981\u4e48\u7528\u6237\u4fe1\u606f\u65e0loading\u77ac\u95f4\u663e\u793a\uff0c\u8981\u4e48\u663e\u793a\u81f3\u5c11200ms\u7684loading\uff0c\u8fd9\u6837\u7684\u4f53\u9a8c\u5c31\u4f1a\u66f4\u7ec6\u81f4\u4e86\u3002<\/p>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u622a\u56fe\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-06_191643.png\" alt=\"loading\u6548\u679c\" width=\"348\" height=\"121\"><\/p>\n<p><strong>\u53ef\u53d6\u6d88\u7684Promise<\/strong><\/p>\n<p>\u627e\u5230\u4e00\u4e2a <code>Promise.race()<\/code> \u65b9\u6cd5\u7684\u6848\u4f8b\uff0c\u51fa\u81eaMichael Clark\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>function timeout(delay) {\r\n  let cancel;\r\n  const wait = new Promise(resolve => {\r\n    const timer = setTimeout(() => resolve(false), delay);\r\n    cancel = () => {\r\n      clearTimeout(timer);\r\n      resolve(true);\r\n    };\r\n  });\r\n  wait.cancel = cancel;\r\n  return wait;\r\n}\r\n\r\nfunction doWork() {\r\n  const workFactor = Math.floor(600*Math.random());\r\n  const work = timeout(workFactor);\r\n  \r\n  const result = work.then(canceled => {\r\n    if (canceled)\r\n      console.log('Work canceled');\r\n    else\r\n      console.log('Work done in', workFactor, 'ms');\r\n      return !canceled;\r\n  });\r\n  result.cancel = work.cancel;\r\n  return result;\r\n}\r\n\r\nfunction attemptWork() {\r\n  const work = doWork();\r\n  return Promise.race([work, timeout(300)])\r\n    .then(done => {\r\n      if (!done)\r\n        work.cancel();\r\n      return (done ? 'Work complete!' : 'I gave up');\r\n  });\r\n}\r\n\r\nattemptWork().then(console.log);<\/pre>\n<p>\u4ee3\u7801\u4e5f\u53ef\u4ee5\u70b9\u51fb\u8fd9\u91cc\u8bbf\u95ee\uff1a<a href=\"https:\/\/gist.github.com\/Mahdhir\/1f6609ea75f45c07801168c18cdb3812#file-promise-race-cancellable-promises-michael-js\" target=\"_blank\" rel=\"nofollow noopener\">promise-race-cancellable-promises-michael.js<\/a> <\/p>\n<p><strong>\u957f\u65f6\u95f4\u6267\u884c\u7684\u6279\u5904\u7406<\/strong><\/p>\n<p>\u4e0b\u9762\u4ee3\u7801\u51fa\u81eaChris Jensen\uff0c\u53ef\u4ee5\u4fdd\u6301\u5e76\u884c\u8bf7\u6c42\u7684\u6570\u91cf\u56fa\u5b9a\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>const _ = require('lodash')\r\n\r\nasync function batchRequests(options) {\r\n    let query = { offset: 0, limit: options.limit };\r\n\r\n    do {\r\n        batch = await model.findAll(query);\r\n        query.offset += options.limit;\r\n\r\n        if (batch.length) {\r\n            const promise = doLongRequestForBatch(batch).then(() => {\r\n                <span class=\"green\">\/\/ Once complete, pop this promise from our array\r\n                \/\/ so that we know we can add another batch in its place<\/span>\r\n                _.remove(promises, p => p === promise);\r\n            });\r\n            promises.push(promise);\r\n\r\n            <span class=\"green\">\/\/ Once we hit our concurrency limit, wait for at least one promise to\r\n            \/\/ resolve before continuing to batch off requests<\/span>\r\n            if (promises.length >= options.concurrentBatches) {\r\n                await Promise.race(promises);\r\n            }\r\n        }\r\n    } while (batch.length);\r\n\r\n    <span class=\"green\">\/\/ Wait for remaining batches to finish<\/span>\r\n    return Promise.all(promises);\r\n}\r\n\r\nbatchRequests({ limit: 100, concurrentBatches: 5 });<\/pre>\n<p>\u4e5f\u53ef\u4ee5\u70b9\u51fb\u8fd9\u91cc\u8bbf\u95ee\uff1a<a href=\"https:\/\/gist.github.com\/Mahdhir\/94cc9a62dc096086b0de44630921e3d4#file-promise-race-batch-requests-chris-js\" target=\"_blank\" rel=\"nofollow noopener\">promise-race-batch-requests-chris-js<\/a><\/p>\n<h4>3. Promise.any()<\/h4>\n<p><code>Promise.any()<\/code>\u9002\u5408\u7528\u5728\u901a\u8fc7\u4e0d\u540c\u8def\u5f84\u8bf7\u6c42\u540c\u4e00\u4e2a\u8d44\u6e90\u7684\u9700\u6c42\u4e0a\u3002<\/p>\n<p>\u4f8b\u5982\uff0cVue3.0\u5728unpkg\u548cjsdelivr\u90fd\u6709\u5728\u7ebf\u7684CDN\u8d44\u6e90\uff0c\u90fd\u662f\u56fd\u5916\u7684CDN\uff0c\u56fd\u5185\u76f4\u63a5\u8c03\u7528\u4e0d\u786e\u5b9a\u54ea\u4e2a\u7ad9\u70b9\u4f1a\u62bd\u98ce\uff0c\u52a0\u8f7d\u6162\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u4e24\u4e2a\u8d44\u6e90\u90fd\u8bf7\u6c42\uff0c\u54ea\u4e2a\u8bf7\u6c42\u5148\u6210\u529f\u5c31\u4f7f\u7528\u54ea\u4e00\u4e2a\u3002<\/p>\n<p>\u6bd4\u65b9\u8bf4unpkg\u7684\u5730\u5740\u662f\uff1ahttps:\/\/unpkg.com\/vue@3.0.11\/dist\/vue.global.js<br \/>\njsdelivr\u7684\u5730\u5740\u662f\uff1ahttps:\/\/cdn.jsdelivr.net\/npm\/vue@3.0.11\/dist\/vue.global.js<\/p>\n<p>\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u4ee3\u7801\u8fdb\u884c\u8bf7\u6c42\uff08\u4f7f\u7528\u52a8\u6001 import \u793a\u610f\uff09\uff1a<\/p>\n<pre>let startTime = +new Date();\r\nlet importUnpkg = import('https:\/\/unpkg.com\/vue@3.0.11\/dist\/vue.runtime.esm-browser.js');\r\nlet importJsdelivr = import('https:\/\/cdn.jsdelivr.net\/npm\/vue@3.0.11\/dist\/vue.runtime.esm-browser.js');\r\nPromise.any([importUnpkg, importJsdelivr]).then(vue => {\r\n  console.log('\u52a0\u8f7d\u5b8c\u6bd5\uff0c\u65f6\u95f4\u662f\uff1a' + (+new Date() - startTime) + 'ms');\r\n  console.log(vue.version);\r\n});<\/pre>\n<p>\u7136\u540e\u5c31\u4f1a\u6709\u4e0b\u56fe\u6240\u793a\u7684\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-07_112535.png\" alt=\"\u6267\u884c\u7ed3\u679c\" width=\"621\" height=\"151\"><\/p>\n<p>83\u6beb\u79d2\u5b8c\u6210\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\uff0c\u4e24\u4e2aJS\u7684\u8bf7\u6c42\u65f6\u95f4\u5dee\u5f02\u662f\u633a\u5927\u7684\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-07_112824.png\" alt=\"\u771f\u5b9e\u8bf7\u6c42\u65f6\u95f4\" width=\"1040\" height=\"204\"><\/p>\n<p>\u53ef\u4ee5\u770b\u5230unpkg\u7684\u8bf7\u6c42\u65f6\u95f4\u957f\u4e86\u5f88\u591a 715ms\uff0c\u6211\u6d4b\u8bd5\u7684\u65f6\u5019\u5076\u5c14\u8fd8\u4f1a\u51fa\u73b01S\u4ee5\u4e0a\u7684\u8bf7\u6c42\u65f6\u957f\u3002<\/p>\n<p>\u4e0d\u8fc7\u6ca1\u5173\u7cfb\uff0c\u6709\u4e86 <code>Promise.any()<\/code> \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u6700\u5feb\u7684\u90a3\u4e00\u4e2a\u3002<\/p>\n<p><code>Promise.any()<\/code>\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff0c\u90a3\u5c31\u662f\u5982\u679c unpkg \u8fd9\u4e2a\u7f51\u7ad9\u6302\u4e86\uff0c\u4e5f\u4e0d\u4f1a\u5f71\u54cd Vue \u8d44\u6e90\u7684\u52a0\u8f7d\uff0c\u56e0\u4e3a\u4e00\u4e2a\u8bf7\u6c42\u5931\u8d25\u4e86\uff0c\u4f1a\u7ee7\u7eed\u8bf7\u6c42\u5176\u4ed6\u7684\u8d44\u6e90\uff0c\u4e5f\u5c31\u662f\u4f1a\u53bb\u8bf7\u6c42 jsdelivr \u7684\u8d44\u6e90\u3002<\/p>\n<p>\u8fd9\u6837\u4fdd\u8bc1\u4e86\u8d44\u6e90\u5c3d\u53ef\u80fd\u53ef\u7528\uff0c\u4f46\u662f\u5c3d\u53ef\u80fd\u4f7f\u7528\u52a0\u8f7d\u6700\u5feb\u7684\u8d44\u6e90\u3002<\/p>\n<p>\u5728\u8fd9\u79cd\u573a\u666f\u4e0b\u5c31\u5f88\u5b9e\u7528\u3002<\/p>\n<h3>\u56db\u3001\u517c\u5bb9\u6027\u3001\u7ed3\u8bed\u6ca1\u6709\u5176\u4ed6<\/h3>\n<p><code>Promise.any()<\/code>\u662f\u540e\u51fa\u6765\u7684\u89c4\u8303\uff0c\u56e0\u6b64\uff0c\u517c\u5bb9\u6027\u76f8\u5bf9\u6ede\u540e\u4e00\u4e9b\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-07_113527.png\" alt=\"Promise.any\u517c\u5bb9\u6027\" width=\"556\" height=\"311\"><\/p>\n<p>Safari 14\u624d\u652f\u6301\uff0c\u56e0\u6b64\uff0c\u76ee\u524d\u751f\u4ea7\u73af\u5883\u8fd8\u4e0d\u80fd\u4f7f\u7528\uff0c\u6bd5\u7adf\u6211\u4eec\u5382\u5b50\u8fd8\u6709\u4e9b\u4ea7\u54c1\u9700\u8981\u517c\u5bb9iOS 9\u5462\u3002<\/p>\n<h4>\u7ed3\u8bed<\/h4>\n<p>\u65e0\u8a00\u4ee5\u5bf9\uff0c\u65e0\u8bdd\u53ef\u8bf4\uff0c\u53ea\u5e0c\u671b\u5404\u4f4d\u5fc3\u75bc\u54e5\u54e5\uff0c\u7136\u540e\u5206\u4eab\uff0c\u8f6c\u53d1\uff0c\u6bd4\u5fc3\u3002<\/p>\n<p><iframe id=\"iframeBili\" src=\"\/\/player.bilibili.com\/player.html?aid=587474048&#038;bvid=BV1hB4y1w7VD&#038;cid=317920043&#038;page=1\" scrolling=\"no\" border=\"0\" frameborder=\"no\" framespacing=\"0\" allowfullscreen=\"true\" width=\"100%\" style=\"max-width:100%;\"> <\/iframe><\/p>\n<p><script>var eleIframeBili = document.querySelector('#iframeBili');\neleIframeBili.height = eleIframeBili.clientWidth * 16 \/ 9;\n<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/2764.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9941\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9941<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/promise-cover.png\" width=\"160\" height=\"104\" alt=\"promise\u5c01\u9762\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u8be6\u7ec6\u4ecb\u7ecdPromise.all()\u3001Promise.race()\u548cPromise.any()\u7684\u5f02\u540c\u4ee5\u53ca\u6700\u9002\u5408\u4f7f\u7528\u7684\u573a\u666f\uff0c\u76f8\u4fe1\u4f1a\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u7684\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1240,1239,904,903],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9941"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=9941"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9941\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}