{"id":313239,"date":"2021-01-18T01:44:46","date_gmt":"2021-01-17T17:44:46","guid":{"rendered":"http:\/\/4563.org\/?p=313239"},"modified":"2021-01-18T01:44:46","modified_gmt":"2021-01-17T17:44:46","slug":"%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%aa-promise-%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=313239","title":{"rendered":"\u8bf7\u6559\u4e00\u4e2a Promise \u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e00\u4e2a Promise \u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rodrick <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5206\u522b\u6709\u4e0b\u9762\u4e24\u4e2a\u5199\u6cd5\uff0c\u533a\u522b\u5c31\u662f\u540e\u8005\u7528\u7684 <code>.catch<\/code> \uff0c\u4e3a\u5565\u540c\u7528\u8c03\u7528\u540e\u6253\u5370\u7ed3\u679c\u4e0d\u4e00\u6837\u5462\uff0c\u524d\u8005\u6253\u5370 <code>1<\/code>\uff0c\u540e\u8005\u6253\u5370 <code>2<\/code><\/p>\n<pre><code>function myPromiseRace(arr) {   return new Promise((resolve, reject) =&gt; {     for (let item of arr) {       item.then(           res =&gt; { resolve(res) },           err =&gt; { reject(err) }         )     }   }) } <\/code><\/pre>\n<pre><code>function myPromiseRace(arr) {   return new Promise((resolve, reject) =&gt; {     for (let item of arr) {       item         .then(           res =&gt; { resolve(res) }         )         .catch(err =&gt; {           reject(err)         })     }   }) } <\/code><\/pre>\n<p>\u8c03\u7528<\/p>\n<pre><code>let p1 = Promise.reject(1),   p2 = Promise.resolve(2),   p3 = Promise.resolve(3)  myPromiseRace([p1, p2, p3]).then(res =&gt; {   console.log(res) }).catch(err =&gt; {   console.log(err); }) <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5158944\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : h503mc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e8c\u79cd\u5199\u6cd5\u9700\u8981 then \u91cc\u7684\u51fd\u6570 reject \u6389\u624d\u4f1a\u89e6\u53d1\u4e0b\u4e00\u4e2a\u7684 catch                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5158945\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : genal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e8c\u4e2a mypromise \u591a\u4e86\u4e2a catch\uff0c\u76f8\u5f53\u4e8e\u591a\u5957\u4e86\u4e00\u5c42\uff0c\u6240\u4ee5\u7b2c\u4e00\u4e2a promise \u66f4\u6162\uff0c\u7531\u4e8e promise \u53ea\u80fd resolve\uff0creject \u4e00\u6b21\uff0c\u8c01\u5feb\u5148\u8f93\u51fa\u8c01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5158946\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : genal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u540e\u9762\u7684 resolve,rejecrt \u662f\u4ec0\u4e48\u4e0d\u91cd\u8981\u4e86\uff0cpromise \u53ea\u80fd resolve\uff0creject \u4e00\u6b21                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5158947\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : faceRollingKB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ece micro task \u7684\u89d2\u5ea6\u770b\uff0cpromise \u672c\u4f53\u5728 current task \u4e2d\u6267\u884c\uff0cthen \u91cc\u9762\u7684\u56de\u8c03\u88ab\u6dfb\u52a0\u5230 micro task \u961f\u5217\u4e2d\uff0c\u800c\u7b2c\u4e8c\u79cd\u5199\u6cd5\u91cc\u9762\u7684 catch \u662f\u5728 then \u6240\u5c5e\u7684 micro task \u88ab\u6267\u884c\u65f6\u624d\u88ab\u6dfb\u52a0\u5230 micro task \u961f\u5217\u7684\uff0c\u6240\u4ee5\uff1a<br \/>\u7b2c\u4e00\u79cd\u5199\u6cd5\uff1a\u4e09\u4e2a promise \u4f9d\u6b21\u88ab\u6dfb\u52a0 then \u91cc\u9762\u5b9a\u4e49\u7684 micro task\uff0c\u5f97\u5230\u961f\u5217 p1-then \u3001p2-then \u3001p3-then\uff0cp1-then \u7684 reject \u56de\u8c03\u4f1a\u8d62\u5f97 race \uff1b<br \/>\u7b2c\u4e8c\u79cd\u5199\u6cd5\uff1a\u4e09\u4e2a promise \u4f9d\u6b21\u88ab\u6dfb\u52a0 then \u91cc\u9762\u5b9a\u4e49\u7684 micro task\uff0cmicro task \u7684\u6267\u884c\u7ed3\u679c\u4f1a\u5f97\u5230\u65b0\u7684 promise\uff0c\u7ee7\u800c\u4f9d\u6b21\u6dfb\u52a0 catch \u91cc\u9762\u7684 micro task\uff0c\u5b8c\u5168\u6267\u884c\u4e0b\u53bb\u4f1a\u6709 6 \u4e2a micro task\uff0c\u4f9d\u6b21\u662f p1-then \u3001p2-then \u3001p3-then \u3001p1-catch \u3001p2-catch \u3001p3-catch\uff0cp1 \u7684 then \u867d\u7136\u6392\u5728\u7b2c 1 \u4f4d\uff0c\u4f46\u53ea\u662f\u8fd4\u56de\u4e86\u4e00\u4e2a\u65b0\u7684 promise \u5e76\u5c06 p1-catch \u6dfb\u52a0\u5230 micro task \u961f\u5217\uff0c\u5e76\u6ca1\u6709\u56de\u8c03 then \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u6240\u4ee5 p2-then \u8d62\u5f97 race \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5158948\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rodrick <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @h503mc \u662f\u6307 p1 \u8fd8\u6ca1 reject \u7684\u65f6\u5019 p2 \u5df2\u7ecf resolve \u4e86\u4e48<br \/>@genal \u5927\u6982\u660e\u767d\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5158949\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rodrick <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @faceRollingKB \u554a\u6211\u660e\u767d\u4e86\uff01\u786e\u5b9e\u662f\u5728\u5fae\u4efb\u52a1\u961f\u5217\u91cc\u662f\u6709\u5148\u540e\u6267\u884c\u987a\u5e8f\u7684\uff0c\u6211\u7ed9\u8fd9\u832c\u5fd8\u4e86\uff0c\u975e\u5e38\u611f\u8c22                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5158950\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : TommyDx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e8c\u79cd\u5199\u6cd5\uff0c\u5728 p1 \u7684 reject \u8fd8\u6ca1\u88ab catch \u7684\u65f6\u5019\uff0cp2 \u7684 resolve \u5df2\u7ecf\u88ab new Promise \u7684 resolve \u51fd\u6570\u8f93\u51fa\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5158951\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cxe2v <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             4 \u8bf4\u7684\u5f88\u8be6\u7ec6\u4e86\uff0c\u6211\u8bf4\u4e0b\u7b80\u5316\u7248\u672c\u7684<\/p>\n<p>\u7b2c\u4e00\u79cd\u5199\u6cd5\uff0c\u662f\u540c\u65f6\u5c06 resolve \u548c reject \u6dfb\u52a0\u5230\u4e86 \u5fae\u4efb\u52a1 \u961f\u5217<br \/>\u7b2c\u4e8c\u79cd\u5199\u6cd5\uff0c\u53ea\u662f\u5c06 resolve \u5148\u6dfb\u52a0\u5230\u4e86\u5fae\u4efb\u52a1\u961f\u5217\uff0c\u7136\u540e\u518d\u4f9d\u6b21\u6dfb\u52a0\u4e86 catch \u5230\u5fae\u4efb\u52a1<\/p>\n<p>\u90a3\u4e48\u7b2c\u4e8c\u79cd\u5199\u6cd5\u5728\u6267\u884c\u7684\u65f6\u5019\uff0c\u80af\u5b9a\u662f\u8c01\u5148 resolve \u8c01\u5c31\u8d62\u4e86\uff0c\u56e0\u4e3a reject \u7684\u90a3\u4e2a catch \u5fae\u4efb\u52a1\u662f\u6392\u5728\u6240\u6709 resolve \u7684\u540e\u9762\u7684                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5158952\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Sparetire <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e00\u79cd\u5199\u6cd5\u4e00\u4e2a tick\uff0c\u7b2c\u4e8c\u79cd\u5199\u6cd5\u4e24\u4e2a tick                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e00\u4e2a Promise \u7684\u95ee\u9898 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/313239"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=313239"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/313239\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313239"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}