{"id":1414,"date":"2022-06-14T12:03:50","date_gmt":"2022-06-14T04:03:50","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1414"},"modified":"2023-02-16T14:44:17","modified_gmt":"2023-02-16T06:44:17","slug":"36-xin-de-promise-fang-faallsettled-any","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1414","title":{"rendered":"36 &#8211; \u65b0\u7684 Promise \u65b9\u6cd5\uff1aallSettled &#038; any"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/07\/16551794469434.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/new-promise-methods-allsettled-any-3ii9\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/new-promise-methods-allsettled-any-3ii9<\/a><\/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\/ES11 \u5f15\u5165\u4e86 <code>Promise.allSettled<\/code>\uff0c\u5b83\u786e\u5b9e\u5f88\u65b0\u540c\u65f6\u4f7f\u7528\u7684\u65f6\u5019\u4e5f\u9700\u8981\u5f53\u5fc3\u3002\u786e\u5b9a\u4e00\u4e0b\u4f60\u6253\u7b97\u652f\u6301\u7684\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301\u8be5\u7279\u6027\u3002<\/p>\n<p>\u5f53\u6240\u6709\u7684 promises \u7ed3\u675f\uff08\u6210\u529f\u6216\u5931\u8d25\uff09\u65f6\uff0c<code>allSettled<\/code> \u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a promise\u3002\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u5bf9\u8c61\u7ec4\u6210\u6570\u7ec4\uff0c\u5e76\u4e14\u5176\u4e2d\u6bcf\u4e2a\u5bf9\u8c61\u63cf\u8ff0\u4e86\u8f93\u5165 promise \u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p><code>Promise.allSettled<\/code> \u4e0e <code>Promise.all<\/code> \u6709\u4e00\u4e9b\u533a\u522b\u3002<\/p>\n<p>\u5f53\u8f93\u5165\u5176\u4e2d\u7684 promise \u6709\u5931\u8d25\u65f6 <code>Promise.all<\/code> \u4f1a\u8fd4\u56de\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5931\u8d25\u7684 promise \u6216\u5168\u90e8\u6210\u529f\u5219\u8fd4\u56de\u6210\u529f\u3002\u6240\u4ee5\u5982\u679c\u6211\u4eec\u5411 <code>promise.all<\/code> \u8f93\u5165\u4e86 5 \u4e2a promise \u800c\u5176\u4e2d\u4e24\u4e2a\u5931\u8d25\uff0c<code>promise.all<\/code> \u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5931\u8d25\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0c <code>promise.allSettled<\/code> \u4f1a\u7b49\u5230\u6240\u6709\u7684 promise \u5b8c\u6210\u5e76\u8fd4\u56de\u6240\u6709\u8f93\u5165 promise \u8fd4\u56de\u7684\u7ed3\u679c\u3002\u5f53\u5f02\u6b65\u7684 promise \u4e4b\u95f4\u4e0d\u4e92\u76f8\u4f9d\u8d56\u53ef\u4ee5\u4f7f\u7528 <code>promise.allSettled<\/code>\uff0c\u540c\u65f6\u4f60\u53ef\u4ee5\u91cd\u8bd5\u5931\u8d25\u7684 promise\u3002\u5982\u679c\u4f60\u7684\u8fc7\u7a0b\u4f9d\u8d56\u6240\u6709\u7684\u5f02\u6b65\u4efb\u52a1\u90fd\u6210\u529f\u624d\u80fd\u7ee7\u7eed\uff0c\u5219\u4f7f\u7528 <code>promise.all<\/code>\u3002<\/p>\n<pre><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>\u4e00\u65e6\u56db\u4e2a promise \u7ed3\u675f\uff0c<code>allSettled<\/code> \u5c06\u4f1a\u8fd4\u56de\u7ed3\u679c\u5230 then \u7684\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u5c06\u4f1a\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre><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>Promise.any<\/code> \u5728\u63d0\u6848\u7684\u7b2c\u4e09\u4e2a\u9636\u6bb5\uff08\u4f5c\u8005\u5199\u6587\u65f6\u662f\u5929\u72b6\u6001\uff0c\u73b0\u5728\u5df2\u7ecf\u901a\u8fc7\uff09\u3002<\/p>\n<p><code>Promise.any<\/code> \u53ef\u4ee5\u63a5\u53d7\u4e00\u5217\u53ef\u8fed\u4ee3\u7684 promise\uff0c\u540c\u65f6\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6210\u529f\u7684 promise\u3002\u5982\u679c\u6ca1\u6709\u4e00\u4e2a promise \u6210\u529f\uff0c\u5219\u8fd4\u56de <code>AggregateError<\/code>\u3002<code>AggregateError<\/code> \u4f1a\u805a\u5408\u6240\u6709\u8f93\u5165\u7684 promise \u8fd4\u56de\u7684\u9519\u8bef\u3002<\/p>\n<p><code>Promise.any<\/code> \u4e0e <code>Promise.all<\/code> \u5b8c\u5168\u76f8\u53cd\u3002<\/p>\n<pre><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>\u5168\u90e8\u9519\u8bef\u7684 promise \u4f8b\u5b50\uff1a<\/p>\n<pre><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","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/new-promise-me&#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":[198,199],"class_list":["post-1414","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-promise-allsettled","tag-promise-any"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1414","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=1414"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1414\/revisions"}],"predecessor-version":[{"id":1415,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1414\/revisions\/1415"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}