{"id":43937,"date":"2023-12-11T15:01:21","date_gmt":"2023-12-11T07:01:21","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=43937"},"modified":"2023-12-11T15:01:21","modified_gmt":"2023-12-11T07:01:21","slug":"promise%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%8c%e5%85%b6%e5%ba%95%e5%b1%82%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%ef%bc%9f-2","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/promise%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%8c%e5%85%b6%e5%ba%95%e5%b1%82%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%ef%bc%9f-2\/","title":{"rendered":"promise\u662f\u4ec0\u4e48\uff0c\u5176\u5e95\u5c42\u5982\u4f55\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u53c2\u8003\u56de\u7b54\uff1a<\/p>\n<p>Promise\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4fdd\u5b58\u7740\u672a\u6765\u5c06\u8981\u7ed3\u675f\u7684\u4e8b\u4ef6\uff0c\u5979\u6709\u4e24\u4e2a\u7279\u5f81:<\/p>\n<p>1\u3001\u5bf9\u8c61\u7684\u72b6\u6001\u4e0d\u53d7\u5916\u90e8\u5f71\u54cd\uff0cPromise\u5bf9\u8c61\u4ee3\u8868\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u6709\u4e09\u79cd\u72b6\u6001\uff0cpending\u8fdb\u884c\u4e2d\uff0cfulfilled\u5df2\u6210\u529f\uff0crejected\u5df2\u5931\u8d25\uff0c\u53ea\u6709\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c\uff0c\u624d\u53ef\u4ee5\u51b3\u5b9a\u5f53\u524d\u662f\u54ea\u4e00\u79cd\u72b6\u6001\uff0c\u4efb\u4f55\u5176\u4ed6\u64cd\u4f5c\u90fd\u65e0\u6cd5\u6539\u53d8\u8fd9\u4e2a\u72b6\u6001\uff0c\u8fd9\u4e5f\u5c31\u662fpromise\u540d\u5b57\u7684\u7531\u6765<\/p>\n<p>2\u3001\u4e00\u65e6\u72b6\u6001\u6539\u53d8\uff0c\u5c31\u4e0d\u4f1a\u518d\u53d8\uff0cpromise\u5bf9\u8c61\u72b6\u6001\u6539\u53d8\u53ea\u6709\u4e24\u79cd\u53ef\u80fd\uff0c\u4ecepending\u6539\u5230fulfilled\u6216\u8005\u4ecepending\u6539\u5230rejected\uff0c\u53ea\u8981\u8fd9\u4e24\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u72b6\u6001\u5c31\u51dd\u56fa\u4e86\uff0c\u4e0d\u4f1a\u518d\u6539\u53d8\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u79f0\u4e3a\u5b9a\u578bresolved,<\/p>\n<p>Promise\u7684\u57fa\u672c\u7528\u6cd5\uff0c<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-javascript&quot;&quot; lang=&quot;&quot;javascript&quot;&quot;&gt;let promise1 = new Promise(function(resolve,reject){<br \/>\n        setTimeout(function(){<br \/>\n        resolve(&#039;ok&#039;)<br \/>\n        },1000)<br \/>\n    })<br \/>\n    promise1.then(function success(val){<br \/>\n     console.log(val)<br \/>\n})<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u6700\u7b80\u5355\u4ee3\u7801\u5b9e\u73b0promise<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-javascript&quot;&quot; lang=&quot;&quot;javascript&quot;&quot;&gt;class PromiseM {<br \/>\n    constructor (process) {<br \/>\n    this.status = &#039;pending&#039;<br \/>\n    this.msg = &#039;&#039;<br \/>\n    process(this.resolve.bind(this), this.reject.bind(this))<br \/>\n    return this<br \/>\n    }<br \/>\n    resolve (val) {<br \/>\n    this.status = &#039;fulfilled&#039;<br \/>\n    this.msg = val<br \/>\n    }<br \/>\n    reject (err) {<br \/>\n    this.status = &#039;rejected&#039;<br \/>\n    this.msg = err<br \/>\n    }<br \/>\n    then (fufilled, reject) {<br \/>\n    if(this.status === &#039;fulfilled&#039;) {<br \/>\n    fufilled(this.msg)<br \/>\n    }<br \/>\n    if(this.status === &#039;rejected&#039;) {<br \/>\n    reject(this.msg)<br \/>\n    }<br \/>\n    }<br \/>\n}<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\/\/\u6d4b\u8bd5\u4ee3\u7801<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-javascript&quot;&quot; lang=&quot;&quot;javascript&quot;&quot;&gt;var mm=new PromiseM(function(resolve,reject){<br \/>\n    resolve(&#039;123&#039;);<br \/>\n    });<br \/>\n    mm.then(function(success){<br \/>\n      console.log(success);<br \/>\n    },function(){<br \/>\n     console.log(&#039;fail!&#039;);<br \/>\n});<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u53c2\u8003\u56de\u7b54\uff1a Promise\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4fdd\u5b58\u7740\u672a\u6765\u5c06\u8981\u7ed3\u675f\u7684\u4e8b\u4ef6\uff0c\u5979\u6709\u4e24\u4e2a\u7279\u5f81: [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-43937","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/43937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=43937"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/43937\/revisions"}],"predecessor-version":[{"id":43938,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/43937\/revisions\/43938"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=43937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=43937"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=43937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}