{"id":1279,"date":"2022-06-14T12:03:50","date_gmt":"2022-06-14T04:03:50","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1279"},"modified":"2023-02-16T14:44:11","modified_gmt":"2023-02-16T06:44:11","slug":"36-xin-de-promise-fang-faallsettled-any-race","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1279","title":{"rendered":"36 &#8211; \u65b0\u7684 Promise \u65b9\u6cd5\uff1aallSettled &#038; any &#038; race"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16551794469434.jpg\" alt=\"\" \/><\/p>\n<h2><a id=\"%E7%AE%80%E4%BB%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b80\u4ecb<\/h2>\n<p>\u5728\u4e4b\u524d\u7684\u4e24\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u8986\u76d6\u4e86\u57fa\u7840\u548c\u9ad8\u7ea7\u7684 promise \u77e5\u8bc6\u3002\u8fd8\u6709\u4e24\u4e2a\u4e0d\u9519\u7684\u65b0\u64cd\u4f5c\u7b26\/\u65b9\u6cd5\uff0c\u53ef\u4ee5\u662f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u91cd\u6e29\u4e0b\u5b83\u4eec\u3002<\/p>\n<h2><a id=\"allsettled\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>allSettled<\/h2>\n<p>ES2020 \u6216 ES11 \u5f15\u5165\u4e86 <code>promise.allSettled<\/code>\uff0c\u6240\u4ee5\u5b83\u662f\u5f88\u65b0\u7684\u65b9\u6cd5\u6240\u4ee5\u4f7f\u7528\u7684\u65f6\u5019\u9700\u8981\u5f53\u5fc3\u3002\u4f7f\u7528\u4e4b\u524d\u9700\u8981\u786e\u5b9a\u4f60\u8981\u652f\u6301\u7684\u6d4f\u89c8\u5668\u7248\u672c\u3002<\/p>\n<p>\u5f53\u6240\u6709\u7684 promise \u8fd4\u56de\u4e0d\u7ba1\u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\uff0c<code>allSettled<\/code> \u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a promise\u3002\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e00\u9879\u63cf\u8ff0\u4e86\u8f93\u5165 promise \u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p><code>allSettled<\/code> \u548c <code>all<\/code> \u6709\u4e00\u4e9b\u533a\u522b\u3002<\/p>\n<p><code>all<\/code> \u4f1a\u8fd4\u56de\u8f93\u5165\u7684 promise \u4e2d\u7b2c\u4e00\u4e2a\u662f\u5931\u8d25\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u5982\u679c\u6211\u4eec\u8f93\u5165\u4e86 5 \u4e2a promise \u5176\u4e2d\u4e24\u4e2a\u5931\u8d25\u4e86\uff0c<code>all<\/code> \u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5931\u8d25\u7684\u7ed3\u679c\u3002<\/p>\n<p><code>allSettled<\/code> \u5219\u4f1a\u7b49\u5f85\u6240\u6709\u7684 promise \u5b8c\u6210\u7136\u540e\u8fd4\u56de\u6240\u6709\u6309\u8f93\u5165\u987a\u5e8f\u5bf9\u5e94\u7684\u7ed3\u679c\u3002\u5f53\u5f02\u6b65\u7684\u4efb\u52a1\u5f7c\u6b64\u6ca1\u6709\u4f9d\u8d56\u5173\u7cfb\u4f7f\u7528 <code>allSettled<\/code> \u7136\u540e\u91cd\u8bd5\u5931\u8d25\u7684\u4efb\u52a1\u3002\u82e5\u4f60\u7684\u6267\u884c\u6b65\u9aa4\u4f9d\u8d56\u6240\u6709\u7684\u5f02\u6b65\u4efb\u52a1\u5219\u4f7f\u7528 <code>all<\/code>\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const promise1 = Promise.resolve(&quot;Parwinder&quot;);\nconst promise2 = new Promise((resolve) =&gt; {\n    setTimeout(() =&gt; {\n        resolve(&quot;Lauren&quot;);\n    }, 2000);\n});\nconst promise3 = Promise.reject(&quot;Robert&quot;);\nconst promise4 = Promise.resolve(&quot;Eliu&quot;);\n\nPromise.allSettled([promise1, promise2, promise3, promise4]).then((data) =&gt; {\n    console.log(data);\n});\n<\/code><\/pre>\n<p>\u4e0a\u9762\u56db\u4e2a\u4efb\u52a1\u4e00\u65e6\u90fd\u7ed3\u675f\uff08\u6210\u529f\u6216\u5931\u8d25\uff09\uff0c<code>allSettled<\/code> \u5219\u4f1a\u5411 then \u7684\u56de\u8c03\u51fd\u6570\u4e2d\u4f20\u9012\u7ed3\u679c\uff0c\u6253\u5370\u5982\u4e0b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">[{\n  status: &quot;fulfilled&quot;,\n  value: &quot;Parwinder&quot;\n}, {\n  status: &quot;fulfilled&quot;,\n  value: &quot;Lauren&quot;\n}, {\n  reason: &quot;Robert&quot;,\n  status: &quot;rejected&quot;\n}, {\n  status: &quot;fulfilled&quot;,\n  value: &quot;Eliu&quot;\n}]\n<\/code><\/pre>\n<h2><a id=\"any\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>any<\/h2>\n<p><code>any<\/code> \u7684\u8f93\u5165\u53c2\u6570\u4e00\u822c\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6bd4\u5982\u6570\u7ec4\uff0c\u5b83\u8fd4\u56de\u6570\u7ec4\u4e2d\u7b2c\u4e00\u4e2a\u6210\u529f promise \u7684\u7ed3\u679c\uff0c\u82e5\u6240\u6709\u7684 promise \u90fd\u5931\u8d25\u5219\u8fd4\u56de <code>AggregateError<\/code> \uff0c<code>AggregateError<\/code> \u662f\u7528\u6765\u7ec4\u5408\u8f93\u5165 promise \u8fd4\u56de\u7684\u72ec\u7acb\u9519\u8bef\u3002<\/p>\n<p><code>any<\/code> \u4e0e <code>all<\/code> \u662f\u76f8\u5bf9\u7684\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const promise1 = Promise.resolve(&quot;Parwinder&quot;);\nconst promise2 = new Promise((resolve) =&gt; {\n    setTimeout(() =&gt; {\n        resolve(&quot;Lauren&quot;);\n    }, 2000);\n});\nconst promise3 = Promise.reject(&quot;Robert&quot;);\nconst promise4 = Promise.resolve(&quot;Eliu&quot;);\n\nPromise.any([promise1, promise2, promise3, promise4]).then((data) =&gt; {\n    console.log(data); \/\/ Parwinder (first successful promise)\n});\n<\/code><\/pre>\n<p>\u5047\u8bbe\u6240\u6709\u7684 promise \u90fd\u8fd4\u56de\u9519\u8bef\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const promise1 = Promise.reject(&quot;Parwinder&quot;);\nconst promise2 = new Promise((resolve,reject) =&gt; {\n    setTimeout(() =&gt; {\n        reject(&quot;Lauren&quot;);\n    }, 2000);\n});\nconst promise3 = Promise.reject(&quot;Robert&quot;);\nconst promise4 = Promise.reject(&quot;Eliu&quot;);\n\nPromise.any([promise1, promise2, promise3, promise4]).then((data) =&gt; {\n    console.log(data); \/\/ &quot;AggregateError: No Promise in Promise.any was resolved&quot;\n});\n<\/code><\/pre>\n<h2><a id=\"race\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>race<\/h2>\n<p><code>race<\/code> \u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6210\u529f\u6216\u5931\u8d25 promise \u7684\u7ed3\u679c\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const promise1 = new Promise((resolve, reject) =&gt; {\n  setTimeout(resolve, 500, 'one');\n});\n\nconst promise2 = new Promise((resolve, reject) =&gt; {\n  setTimeout(resolve, 100, 'two');\n});\n\nPromise.race([promise1, promise2]).then((value) =&gt; {\n  console.log(value);\n  \/\/ Both resolve, but promise2 is faster\n});\n\/\/ expected output: &quot;two&quot;\n<\/code><\/pre>\n<p>\u82e5\u4f20\u9012\u7ed9 <code>race<\/code> \u7684\u53c2\u6570\u4e3a\u7a7a\u6570\u7ec4\uff0c\u5219\u6c38\u8fdc\u4e0d\u4f1a\u6709\u7ed3\u679c\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">var foreverPendingPromise = Promise.race([]);\nconsole.log(foreverPendingPromise);\nsetTimeout(function(){\n    console.log('the stack is now empty');\n    console.log(foreverPendingPromise);\n});\n\n\/\/ logs, in order:\n\/\/ Promise { &lt;state&gt;: &quot;pending&quot; }\n\/\/ the stack is now empty\n\/\/ Promise { &lt;state&gt;: &quot;pending&quot; }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u5728\u4e4b\u524d\u7684\u4e24\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u8986\u76d6\u4e86\u57fa\u7840\u548c\u9ad8\u7ea7\u7684 promise \u77e5\u8bc6\u3002\u8fd8\u6709\u4e24\u4e2a\u4e0d\u9519\u7684\u65b0\u64cd\u4f5c\u7b26\/&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[249,2,20,3],"tags":[152,150,6,86,151],"class_list":["post-1279","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-allsettled","tag-any","tag-javascript","tag-promise","tag-race"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1279"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1279\/revisions"}],"predecessor-version":[{"id":1281,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1279\/revisions\/1281"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}