{"id":161949,"date":"2020-09-26T20:18:22","date_gmt":"2020-09-26T12:18:22","guid":{"rendered":"http:\/\/4563.org\/?p=161949"},"modified":"2020-09-26T20:18:22","modified_gmt":"2020-09-26T12:18:22","slug":"promise-%e5%ad%98%e5%88%b0%e5%8f%98%e9%87%8f%e4%b8%ad%e4%b8%ba%e4%bb%80%e4%b9%88%e5%8f%af%e4%bb%a5%e9%98%b2%e6%ad%a2%e9%98%bb%e5%a1%9e","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=161949","title":{"rendered":"promise \u5b58\u5230\u53d8\u91cf\u4e2d\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u9632\u6b62\u963b\u585e?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  promise \u5b58\u5230\u53d8\u91cf\u4e2d\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u9632\u6b62\u963b\u585e?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : siwadiya <\/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><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/10\/20200930_5f74d19867702.png\" alt=\"promise \u5b58\u5230\u53d8\u91cf\u4e2d\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u9632\u6b62\u963b\u585e?\" \/><\/p>\n<p>\u4e3a\u4ec0\u4e48\u5c06 promise \u5b58\u5230\u53d8\u91cf\u4e2d,\u8fd9\u4e09\u4e2a promise \u5c31\u4f1a\u540c\u65f6\u8fd0\u884c,\u800c\u4e0d\u662f\u963b\u585e\u540e\u9762\u4ee3\u7801\u7684\u8fd0\u884c<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>11<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3514639\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : abelmakihara <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u56e0\u4e3a\u662f await \u963b\u585e\u5728\u90a3\u7684<br \/>\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u662f\u6267\u884c\u5b8c\u4e86\u518d\u4e00\u8d77\u62ff\u7ed3\u679c\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514640\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : noe132 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u56e0\u4e3a\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u7b49\u540c\u4e8e<br \/>const timeoutPromise1 = timeoutPromise(3000)<br \/>await timeoutPromise1<br \/>const timeoutPromise2 = timeoutPromise(3000)<br \/>await timeoutPromise2<br \/>const timeoutPromise3 = timeoutPromise(3000)<br \/>await timeoutPromise3<\/p>\n<p>\u800c\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u56e0\u4e3a\u90fd\u662f\u7b49\u5f85 3 \u79d2\uff0c\u6240\u4ee5 await timeoutPromise1 \u65f6\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a\u7684\u65f6\u95f4\u4e5f\u5230\u4e86\u3002<br \/>\u4f60\u8bd5\u8bd5 \u5982\u679c\u65f6\u95f4\u4e0d\u4e00\u6837\u4f1a\u600e\u4e48\u6837<\/p>\n<p>const timeoutPromise1 = timeoutPromise(3000)<br \/>const timeoutPromise2 = timeoutPromise(4000)<br \/>const timeoutPromise3 = timeoutPromise(5000)<br \/>await timeoutPromise1<br \/>print(&#8216;promise 1 done&#8217;)<br \/>await timeoutPromise2<br \/>print(&#8216;promise 2 done&#8217;)<br \/>await timeoutPromise3<br \/>print(&#8216;promise 3 done&#8217;)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514641\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Curtion <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7b2c\u4e00\u4e2a timeoutPromise \u5728 resolve \u540e\u624d\u4f1a\u7ee7\u7eed\u6ce8\u518c\u7b2c\u4e8c\u4e2a timeoutPromise \u5fae\u4efb\u52a1\uff1b\u800c\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u5728\u5f53\u524d Event Loop \u4e2d\u5c31\u4e00\u6b21\u6027\u6ce8\u518c\u4e86\u4e09\u4e2a\u5fae\u4efb\u52a1\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514642\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wxsm <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             #2 \u8bf4\u5f97\u5f88\u597d\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514643\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : siwadiya <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @abelmakihara \u53ef\u4ee5\u8fd9\u4e48\u7406\u89e3\u5417: \u8fd9\u4e09\u4e2a promise \u662f\u540c\u6b65\u8fd0\u884c\u7684,\u540e\u9762\u7684 await \u662f\u4e3a\u4e86\u5c06 pending \u66f4\u6539\u4e3a fulfilled                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514644\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : abelmakihara <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @siwadiya \u5f53\u7136\u4e0d\u662f\u4e86 \u662f await \u5728\u7b49\u5f85\u7740\u72b6\u6001\u53d8\u66f4\u7684\u8fd4\u56de\u7ed3\u679c \u56e0\u679c\u53cd\u4e86<br \/>\u4ed6\u4eec\u90fd\u662f&#8217;\u540c\u6b65&#8217;\u8fd0\u884c\u4e86 3s<br \/>\u6240\u4ee5 await \u62ff\u7ed3\u679c\u7684\u65f6\u5019\u975e\u5e38\u5feb<br \/>\u4e0d\u7136\u50cf#2 \u7684\u90a3\u6837<br \/>await timeoutPromise2\/3 \u7684\u65f6\u5019\u8fd8\u662f\u9700\u8981\u518d\u5404\u7b49\u5f85 1s \u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514645\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Reapper <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             #2 \u5df2\u7ecf\u5f88\u8be6\u7ec6\u4e86\uff0c\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u662f 3s 3s 3s\uff0c\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5148\u6267\u884c\u4e86 3 \u4e2a promise\uff0c\u7136\u540e\u5f00\u59cb\u7b49\u5f85\uff0c3s \u540e\u6709\u7ed3\u679c\u4e86\uff0c\u7136\u540e\u8f93\u51fa \u8f93\u51fa \u8f93\u51fa\uff0casync\/await \u53ea\u662f\u4e00\u4e2a\u8bed\u6cd5\u7cd6\uff0c\u5efa\u8bae\u7406\u89e3\u4e00\u4e0b promise \u7684\u6267\u884c\u8fc7\u7a0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514646\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yzqtdu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e2a\u4eba\u89c2\u70b9\uff0c\u8fd9\u91cc\u4e0d\u5b58\u5728\u6240\u8c13\u7684\u963b\u585e\uff0cgenerator \u5728 yield \u4e4b\u540e\u963b\u585e\u4e86\u540e\u7eed\u547d\u4ee4\u7684\u6267\u884c\u5417\uff1f\u6ca1\u6709\uff0c\u6267\u884c\u6d41\u53ea\u662f\u5207\u6362\u5230\u5176\u4ed6\u6808\u53bb\u4e86\uff0c\u5728\u5408\u9002\u7684\u65f6\u673a\u53ef\u4ee5\u8df3\u56de yield \u4e4b\u540e\u7684\u547d\u4ee4\u3002async\/await promise \u8fd9\u4e9b\u90fd\u4e0d\u963b\u585e\u6267\u884c\u6d41\uff0c\u8981\u8bf4\u963b\u585e\uff0c\u540c\u6b65\u963b\u585e io \u6216\u8005 sleep \u624d\u662f\u771f\u7684\u963b\u585e\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514647\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SoloCompany <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u5e0c\u671b\u53bb\u6389\u65e0\u610f\u4e49\u7684\u8fd4\u56de\u503c<\/p>\n<p>\u53ef\u4ee5\u7528 await Promise.all                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514648\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jirajine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u56e0\u4e3a js \u7684 promise \u662f eager \u7684\uff0c\u521b\u5efa\u65f6\u5c31\u5df2\u7ecf\u88ab\uff08\u8c03\u5ea6\uff09\u6267\u884c\u4e86\u3002<br \/>\u800c rust \u8fd9\u6837\u7684\u5219\u662f lazy \u7684\uff0c\u4f60\u4e0d await \u5b83\u5c31\u5b8c\u5168\u4e0d\u4f1a\u6267\u884c\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e24\u8005\u5c31\u4e00\u6837\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3514649\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b80\u5355\u7406\u89e3\u7684\u8bdd\uff1a\u51fd\u6570\u540e\u9762\u52a0\u62ec\u53f7\u5c31\u662f\u8c03\u7528\u51fd\u6570\u7684\u8bed\u6cd5\uff0c\u7b2c\u4e00\u79cd\u662f\u5728 await \u540e\u8c03\u7528\uff0c\u7b2c\u4e8c\u79cd\u662f\u5148\u8c03\u7528\u518d\u7528 await \u63a5\u6536\u7ed3\u679c\u3002\u6240\u4ee5\u7b2c\u4e00\u79cd\u662f\u7b49\u524d\u4e00\u4e2a timeoutPromise \u6267\u884c\u5b8c\u8fd4\u56de\u7ed3\u679c\u540e\u518d\u8c03\u7528\u6267\u884c\u4e0b\u4e00\u4e2a\u51fd\u6570\uff1b\u7b2c\u4e8c\u79cd\u662f\u4e09\u4e2a\u51fd\u6570\u4f9d\u6b21\u5f00\u59cb\u6267\u884c\uff0c\u6267\u884c\u7684\u8fc7\u7a0b\u662f\u540c\u65f6\u8fdb\u884c\u7684\uff0c\u5728 await \u90e8\u5206\u662f\u7b49\u5f85\u524d\u4e00\u4e2a\u6267\u884c\u5b8c\u8fd4\u56de\u7ed3\u679c\u540e\u624d\u7b49\u5f85\u4e0b\u4e00\u4e2a\u6267\u884c\u5b8c\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p>\u201c\u5b58\u5230\u53d8\u91cf\u201d\u4e0d\u662f\u672c\u8d28\u5f71\u54cd\u8fd9\u4e2a\u8fc7\u7a0b\u7684\u56e0\u7d20\uff0c\u800c\u662f\u4e00\u79cd\u8868\u8c61\uff0c\u672c\u8d28\u662f\u4f60\u7684\u5f02\u6b65\u51fd\u6570\u4ec0\u4e48\u65f6\u5019\u88ab\u8c03\u7528\u7684\u3002\u53ef\u4ee5\u53bb\u770b\u770b Promise \u7684\u6267\u884c\u8fc7\u7a0b\uff0cawait \u4e00\u4e2a Promise \u5bf9\u8c61\u5c31\u662f\u73b0\u5728\u6709\u7ed3\u679c\u5c31\u73b0\u5728\u62ff\uff0c\u73b0\u5728\u6ca1\u6709\u7ed3\u679c\u5c31\u7b49\u7740\u7ed3\u679c\u51fa\u6765\uff0c\u7ed3\u679c\u51fa\u6765\u62ff\u5230\u540e\u518d\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>promise \u5b58\u5230\u53d8\u91cf\u4e2d\u4e3a\u4ec0\u4e48\u53ef&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\/161949"}],"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=161949"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/161949\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161949"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}