{"id":2083,"date":"2025-02-26T23:43:01","date_gmt":"2025-02-26T15:43:01","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2083"},"modified":"2025-02-26T23:43:01","modified_gmt":"2025-02-26T15:43:01","slug":"javascript-promise","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-promise\/","title":{"rendered":"JavaScript Promise"},"content":{"rendered":"\n<p><code>Promise<\/code> \u662f JavaScript \u4e2d\u7684\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c\u3002\u5b83\u4ee3\u8868\u4e00\u4e2a\u5728\u672a\u6765\u53ef\u80fd\u5b8c\u6210\u6216\u5931\u8d25\u7684\u64cd\u4f5c\u7684\u6700\u7ec8\u503c\u3002<code>Promise<\/code> \u7684\u5f15\u5165\u4f7f\u5f97 JavaScript \u7684\u5f02\u6b65\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\uff0c\u5e76\u4e14\u6bd4\u4f20\u7edf\u7684\u56de\u8c03\u51fd\u6570\u66f4\u52a0\u6613\u4e8e\u7ef4\u62a4\u548c\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1\u3001<strong>Promise \u7684\u4e09\u79cd\u72b6\u6001<\/strong><\/h3>\n\n\n\n<p>\u4e00\u4e2a <code>Promise<\/code> \u5bf9\u8c61\u6709\u4e09\u79cd\u72b6\u6001\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Pending\uff08\u5f85\u5b9a\uff09<\/strong>\uff1aPromise \u5bf9\u8c61\u7684\u521d\u59cb\u72b6\u6001\uff0c\u8868\u793a\u5f02\u6b65\u64cd\u4f5c\u5c1a\u672a\u5b8c\u6210\u3002<\/li>\n\n\n\n<li><strong>Resolved\/Fulfilled\uff08\u5df2\u5b8c\u6210\uff09<\/strong>\uff1a\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\uff0c\u4e14\u8fd4\u56de\u4e86\u671f\u671b\u7684\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>Rejected\uff08\u5df2\u62d2\u7edd\uff09<\/strong>\uff1a\u5f02\u6b65\u64cd\u4f5c\u5931\u8d25\uff0c\u4e14\u8fd4\u56de\u4e86\u9519\u8bef\u539f\u56e0\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u4e9b\u72b6\u6001\u5728 Promise \u5185\u90e8\u7ba1\u7406\uff0c\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2\u3001<strong>Promise \u7684\u57fa\u672c\u7528\u6cd5<\/strong><\/h3>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a <code>Promise<\/code> \u5bf9\u8c61\u65f6\uff0c\u4f20\u5165\u4e00\u4e2a\u6267\u884c\u5668\u51fd\u6570\uff08executor function\uff09\u3002\u8fd9\u4e2a\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\uff1a<code>resolve<\/code> \u548c <code>reject<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u8868\u793a\u64cd\u4f5c\u6210\u529f\u548c\u5931\u8d25\u65f6\u7684\u5904\u7406\u51fd\u6570\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>let promise = new Promise(function(resolve, reject) {\n  let success = true;  \/\/ \u6a21\u62df\u4e00\u4e2a\u6210\u529f\u7684\u64cd\u4f5c\n\n  if (success) {\n    resolve(\"Operation was successful!\");\n  } else {\n    reject(\"Operation failed!\");\n  }\n});\n\npromise.then(function(result) {\n  console.log(result);  \/\/ \u8f93\u51fa \"Operation was successful!\"\n}).catch(function(error) {\n  console.log(error);   \/\/ \u5982\u679c\u5931\u8d25\u8f93\u51fa \"Operation failed!\"\n});<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>resolve()<\/code><\/strong>\uff1a\u5f53\u5f02\u6b65\u64cd\u4f5c\u6210\u529f\u65f6\u8c03\u7528\uff0c\u5c06 Promise \u72b6\u6001\u4ece <code>pending<\/code> \u6539\u4e3a <code>resolved<\/code>\u3002<\/li>\n\n\n\n<li><strong><code>reject()<\/code><\/strong>\uff1a\u5f53\u5f02\u6b65\u64cd\u4f5c\u5931\u8d25\u65f6\u8c03\u7528\uff0c\u5c06 Promise \u72b6\u6001\u4ece <code>pending<\/code> \u6539\u4e3a <code>rejected<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3\u3001<strong>Promise \u7684\u94fe\u5f0f\u8c03\u7528<\/strong><\/h3>\n\n\n\n<p>\u4e00\u4e2a <code>Promise<\/code> \u53ef\u4ee5\u901a\u8fc7 <code>.then()<\/code> \u65b9\u6cd5\u5904\u7406\u6210\u529f\u7684\u7ed3\u679c\uff0c\u6216\u8005\u901a\u8fc7 <code>.catch()<\/code> \u65b9\u6cd5\u5904\u7406\u5931\u8d25\u7684\u9519\u8bef\u3002 <code>.then()<\/code> \u548c <code>.catch()<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u4e5f\u662f <code>Promise<\/code> \u5bf9\u8c61\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u8c03\u7528\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>let promise = new Promise(function(resolve, reject) {\n  resolve(\"Data fetched successfully!\");\n});\n\npromise\n  .then(function(result) {\n    console.log(result);  \/\/ \u8f93\u51fa \"Data fetched successfully!\"\n    return \"Next step\";  \/\/ \u8fd4\u56de\u65b0\u7684\u6570\u636e\n  })\n  .then(function(result) {\n    console.log(result);  \/\/ \u8f93\u51fa \"Next step\"\n  })\n  .catch(function(error) {\n    console.log(error);  \/\/ \u5982\u679c\u6709\u9519\u8bef\u5219\u8f93\u51fa\n  });<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>.then()<\/code><\/strong>\uff1a\u5904\u7406 Promise \u6210\u529f\u7684\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong><code>.catch()<\/code><\/strong>\uff1a\u5904\u7406 Promise \u62d2\u7edd\uff08\u5931\u8d25\uff09\u7684\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u901a\u8fc7\u94fe\u5f0f\u8c03\u7528\u6765\u4f9d\u6b21\u5904\u7406\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4\u3001<strong>Promise.all()<\/strong><\/h3>\n\n\n\n<p><code>Promise.all()<\/code> \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u5305\u542b\u591a\u4e2a Promise \u5bf9\u8c61\u7684\u6570\u7ec4\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Promise\u3002\u8fd9\u4e2a\u65b0\u7684 Promise \u5728\u6240\u6709\u8f93\u5165\u7684 Promise \u90fd\u53d8\u4e3a <code>resolved<\/code> \u65f6\u53d8\u4e3a <code>resolved<\/code>\uff0c\u5982\u679c\u5176\u4e2d\u6709\u4efb\u4f55\u4e00\u4e2a Promise \u53d8\u4e3a <code>rejected<\/code>\uff0c\u5219\u8fd4\u56de <code>rejected<\/code>\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>let promise1 = new Promise(function(resolve, reject) {\n  setTimeout(resolve, 1000, 'First');\n});\nlet promise2 = new Promise(function(resolve, reject) {\n  setTimeout(resolve, 2000, 'Second');\n});\n\nPromise.all(&#91;promise1, promise2])\n  .then(function(values) {\n    console.log(values);  \/\/ \u8f93\u51fa &#91;\"First\", \"Second\"]\n  })\n  .catch(function(error) {\n    console.log(error);\n  });<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Promise.all()<\/code> \u5728\u6240\u6709\u7684 Promise \u90fd\u6210\u529f\u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u7ed3\u679c\u7684\u6570\u7ec4\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4efb\u4f55\u4e00\u4e2a Promise \u5931\u8d25\uff0c\u6574\u4e2a <code>Promise.all()<\/code> \u4f1a\u7acb\u5373\u88ab\u62d2\u7edd\uff0c\u5e76\u4e14\u4f1a\u6267\u884c <code>catch()<\/code> \u5904\u7406\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5\u3001<strong>Promise.race()<\/strong><\/h3>\n\n\n\n<p><code>Promise.race()<\/code> \u65b9\u6cd5\u4e5f\u63a5\u6536\u4e00\u4e2a Promise \u6570\u7ec4\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Promise\u3002\u8fd9\u4e2a\u65b0\u7684 Promise \u4f1a\u5728\u8f93\u5165\u7684 Promise \u4e2d\u6700\u5148\u5b8c\u6210\u7684 Promise \u5b8c\u6210\u65f6\u7acb\u5373\u53d8\u4e3a <code>resolved<\/code> \u6216 <code>rejected<\/code>\uff0c\u4e0d\u7ba1\u5176\u5b83\u7684 Promise \u662f\u5426\u5df2\u7ecf\u5b8c\u6210\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>let promise1 = new Promise(function(resolve, reject) {\n  setTimeout(resolve, 1000, 'First');\n});\nlet promise2 = new Promise(function(resolve, reject) {\n  setTimeout(resolve, 2000, 'Second');\n});\n\nPromise.race(&#91;promise1, promise2])\n  .then(function(value) {\n    console.log(value);  \/\/ \u8f93\u51fa \"First\"\uff0c\u56e0\u4e3a\u5b83\u7b2c\u4e00\u4e2a\u5b8c\u6210\n  })\n  .catch(function(error) {\n    console.log(error);\n  });<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Promise.race()<\/code> \u53ea\u4f1a\u8fd4\u56de\u6700\u5148\u5b8c\u6210\u7684 Promise\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6\u3001<strong>Promise.finally()<\/strong><\/h3>\n\n\n\n<p><code>finally()<\/code> \u65b9\u6cd5\u5141\u8bb8\u4f60\u6307\u5b9a\u4e00\u4e2a\u65e0\u8bba <code>Promise<\/code> \u6700\u7ec8\u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\u90fd\u4f1a\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002\u901a\u5e38\u7528\u4e8e\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>let promise = new Promise(function(resolve, reject) {\n  resolve(\"Task completed\");\n});\n\npromise\n  .then(function(result) {\n    console.log(result);\n  })\n  .catch(function(error) {\n    console.log(error);\n  })\n  .finally(function() {\n    console.log(\"This will run no matter what\");\n  });<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>finally()<\/code> \u65b9\u6cd5\u65e0\u8bba Promise \u72b6\u6001\u5982\u4f55\uff0c\u90fd\u4f1a\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Promise<\/code><\/strong> \u662f JavaScript \u7528\u6765\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u5bf9\u8c61\uff0c\u5b83\u6709\u4e09\u79cd\u72b6\u6001\uff1a<code>pending<\/code>\u3001<code>resolved<\/code>\uff08\u6210\u529f\uff09\u548c <code>rejected<\/code>\uff08\u5931\u8d25\uff09\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <strong><code>then()<\/code><\/strong> \u5904\u7406\u6210\u529f\u7684\u7ed3\u679c\uff0c<strong><code>catch()<\/code><\/strong> \u5904\u7406\u5931\u8d25\u7684\u60c5\u51b5\uff0c<strong><code>finally()<\/code><\/strong> \u7528\u4e8e\u6267\u884c\u6e05\u7406\u5de5\u4f5c\u3002<\/li>\n\n\n\n<li><strong><code>Promise.all()<\/code><\/strong> \u548c <strong><code>Promise.race()<\/code><\/strong> \u7528\u4e8e\u5904\u7406\u591a\u4e2a Promise\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Promise \u662f JavaScript \u4e2d\u7684\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c\u3002\u5b83\u4ee3\u8868\u4e00\u4e2a\u5728\u672a\u6765\u53ef\u80fd\u5b8c\u6210\u6216\u5931\u8d25 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2083","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2083"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2083\/revisions"}],"predecessor-version":[{"id":2084,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2083\/revisions\/2084"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}