{"id":1268,"date":"2022-06-12T11:22:16","date_gmt":"2022-06-12T03:22:16","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1268"},"modified":"2023-02-16T14:44:02","modified_gmt":"2023-02-16T06:44:02","slug":"34-promise-jian-jie","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1268","title":{"rendered":"34 &#8211; Promise \u7b80\u4ecb"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16550041471951.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/promises-introduction-5d30\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/promises-introduction-5d30<\/a><\/p>\n<h2><a id=\"%E5%AF%BC%E8%AF%BB\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5bfc\u8bfb<\/h2>\n<p>\u56de\u8c03\u51fd\u6570\u5bf9\u4e8e\u5904\u7406\u8017\u65f6\u4efb\u52a1\u6216\u540e\u7eed\u4f1a\u88ab\u963b\u585e\u7684\u64cd\u4f5c\u5f88\u6709\u5e2e\u52a9\uff0c\u4f46\u6211\u4eec\u4e5f\u770b\u5230\u4e86\u5b83\u7684\u4e00\u4e9b\u7f3a\u70b9\uff0c\u7279\u522b\u662f\u56de\u5230\u5730\u72f1\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u56de\u8c03\u51fd\u6570\u7684\u5f0a\u7aef\uff0c\u5f15\u5165\u4e86 promise\uff0c\u5b83\u4fe9\u4e4b\u95f4\u7684\u5173\u952e\u533a\u522b\u662f\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u56de\u8c03\uff0c\u6211\u4eec\u53ef\u4ee5\u5411\u51fd\u6570\u4e2d\u4f20\u9012\u4e00\u4e2a\u56de\u8c03\uff0c\u7136\u540e\u7b49\u5230\u6267\u884c\u56de\u8c03\u51fd\u6570\u540e\u53ef\u4ee5\u5f97\u5230\u7ed3\u679c\u3002\u800c\u5728 promise \u4e2d\uff0c\u6211\u4eec\u662f\u5728 promise \u7684\u8fd4\u56de\u503c\u4e2d\u4f7f\u7528\u56de\u8c03\u3002<\/p>\n<h2><a id=\"%E4%BC%98%E5%8A%BF\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f18\u52bf<\/h2>\n<ol>\n<li>Promise \u548c \u56de\u8c03\u51fd\u6570\u5f53\u5728\u5f02\u6b65\u64cd\u4f5c\u4e2d\u4f7f\u7528\u65f6\uff0c\u90fd\u662f\u4e3a\u4e86\u89e3\u51b3\u76f8\u540c\u7684\u95ee\u9898\uff1bPromise \u52a0\u4e86\u4e00\u5c42\u62bd\u8c61\u4f7f\u5f97\u4ee3\u7801\u66f4\u6574\u6d01\u3001\u51fd\u6570\u5316\u4ee5\u53ca\u66f4\u5c11\u7684\u9519\u8bef\u53d1\u751f\uff1b<\/li>\n<li>\u6211\u4eec\u4e0d\u9700\u8981\u77e5\u9053\u5c06\u4f7f\u7528\u5f02\u6b65\u64cd\u4f5c\u8fd4\u56de\u7684\u503c\u7684\u56de\u8c03\uff1b<\/li>\n<li>Promise \u662f\u94fe\u5f0f\u8c03\u7528\u53ef\u4ee5\u662f\u4ee3\u7801\u7ed3\u6784\u6241\u5e73\u5316\u800c\u4e0d\u4f1a\u5f15\u8d77\u56de\u8c03\u5730\u72f1\u95ee\u9898\uff1b<\/li>\n<li>Promise \u5185\u7f6e\u4e86\u9519\u8bef\u5904\u7406\u673a\u5236\uff1b<\/li>\n<\/ol>\n<h2><a id=\"%E5%88%9B%E5%BB%BA\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u521b\u5efa<\/h2>\n<p>\u6211\u4eec\u4f7f\u7528 <code>Promise<\/code> \u6784\u9020\u51fd\u6570\u6765\u521b\u5efa promise:<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const myPromise = new Promise();\n<\/code><\/pre>\n<p>\u4e00\u4e2a promise \u5c31\u50cf\u4e00\u5f20\u6536\u636e\u8bf4\u6211\u5c06\u6765\u9700\u8981\u4f60\u7684\u503c\uff0cPromise \u4e00\u65e6\u5b8c\u6210\uff08resolve \u8fd4\u56de\u6210\u529f\u4fe1\u606f\u6216 reject \u8fd4\u56de\u9519\u8bef\u4fe1\u606f\uff09\uff0c\u6211\u4eec\u5c06\u4f1a\u6267\u884c\u540e\u7eed\u52a8\u4f5c\uff08\u4f8b\u5982\u4ece\u540e\u7aef\u83b7\u53d6\u5458\u5de5\u4fe1\u606f\uff09\u3002<\/p>\n<h2><a id=\"resolve\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Resolve<\/h2>\n<p>promise \u4e3a\u4e86\u5b8c\u6210\u5f02\u6b65\u4efb\u52a1\uff0c\u65f6\u95f4\u662f\u4e0d\u56fa\u5b9a\u7684\u3002\u5f53\u5f02\u6b65\u4efb\u52a1\u8fd8\u5728\u6267\u884c\u7684\u65f6\u5019\uff0cpromise \u5904\u5728 pending \u72b6\u6001\u3002\u4e00\u65e6\u5b8c\u6210\u5b83\u5c06\u8fd4\u56de\u503c\uff08\u901a\u5e38\u662f\u4ece\u5f02\u6b65\u4efb\u52a1\u8fd4\u56de\u7684\uff09\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const myPromise = new Promise((resolve) =&gt; {\n    setTimeout(() =&gt; {\n        resolve(&quot;finished async operation&quot;);\n    }, 2000);\n});\n\nmyPromise.then((response) =&gt; {\n    console.log(response); \/\/ finished async operation\n});\n<\/code><\/pre>\n<p>\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a<\/p>\n<ol>\n<li>Promise \u63a5\u53d7\u4e86\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff1b<\/li>\n<li>\u56de\u8c03\u51fd\u6570\u5185\u90e8\u6267\u884c\u4e86\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\uff1b<\/li>\n<li>\u82e5\u4efb\u52a1\u5b8c\u6210 promise \u5c06\u4f1a resolved\uff1b<\/li>\n<li>\u6211\u4eec\u4f7f\u7528 setTimeout \u6765\u6a21\u62df\u8017\u65f6 2s \u7684\u5f02\u6b65\u4efb\u52a1\uff1b<\/li>\n<li>\u5f53 2s \u540e \u6216\u5f02\u6b65\u4efb\u52a1\u5b8c\u6210\uff0c\u6211\u4eec\u5c06\u5f97\u5230\u6210\u529f\u7684\u4fe1\u606f\u6216\u4ece\u540e\u7aef\u8fd4\u56de\u7684\u6570\u636e\u3002<\/li>\n<\/ol>\n<h2><a id=\"reject\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Reject<\/h2>\n<p>\u6709\u65f6\u5019\u5f02\u6b65\u4efb\u52a1\u5e76\u4e0d\u603b\u662f\u6309\u9884\u671f\u8fd4\u56de\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u9519\u8bef\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528 <code>reject<\/code> \u6765\u63d0\u793a\u5931\u8d25\u4fe1\u606f\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const myPromise = new Promise((resolve, reject) =&gt; {\n    setTimeout(() =&gt; {\n        reject(&quot;an error has occurred&quot;);\n    }, 2000)\n});\n\nmyPromise.then((response) =&gt; {\n    console.log(response);\n}, (error) =&gt; {\n    console.log(error); \/\/ an error has occurred\n});\n<\/code><\/pre>\n<p>promise \u4e2d\u7684\u56de\u8c03\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a<code>resolve<\/code> \u548c <code>reject<\/code>\u3002promise \u4e2d\u7684<code>then<\/code> \u64cd\u4f5c\u7b26\u80fd\u5904\u7406\u4e24\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u5904\u7406\u6210\u529f\u7684\uff08resolve\uff09\uff0c\u7b2c\u4e8c\u4e2a\u662f\u5904\u7406\u5931\u8d25\u7684\uff08reject\uff09\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>myPromise<\/code> 2 \u79d2\u540e\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\uff0c\u53ea\u8981\u4f7f\u7528\u5b83\u7684\u90fd\u4f1a\u5f97\u5230\u63d0\u793a\uff1a\u201can error has occurred\u201d\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u53ea\u662f\u7b80\u5355\u7684\u4ecb\u7ecd\u4e86 promise \u7684\u4e00\u4e9b\u77e5\u8bc6\uff0c\u5728\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u91cd\u6e29\u94fe\u5f0f\u64cd\u4f5c\u3001\u9519\u8bef\u5904\u7406\u4ee5\u53ca\u5e76\u884c\u6267\u884c promise\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/\u524d\u7aef\u9ed1\u677f\u62a5-6.jpg\" alt=\"\u524d\u7aef\u9ed1\u677f\u62a5\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/promises-intro&#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":[6,86,145,144],"class_list":["post-1268","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-javascript","tag-promise","tag-reject","tag-resolve"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1268","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=1268"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1268\/revisions"}],"predecessor-version":[{"id":1269,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1268\/revisions\/1269"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}