{"id":191304,"date":"2020-05-28T08:11:28","date_gmt":"2020-05-28T00:11:28","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=191304"},"modified":"2020-05-17T16:12:35","modified_gmt":"2020-05-17T08:12:35","slug":"introduction-to-es6-2","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/introduction-to-es6-2.html","title":{"rendered":"ES6 Promise \u5bf9\u8c61\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nES6\uff0c \u5168\u79f0 ECMAScript 6.0 \uff0c\u662f JavaScript \u7684\u4e0b\u4e00\u4e2a\u7248\u672c\u6807\u51c6\uff0c2015.06 \u53d1\u7248\u3002ES6 \u4e3b\u8981\u662f\u4e3a\u4e86\u89e3\u51b3 ES5 \u7684\u5148\u5929\u4e0d\u8db3\uff0c\u6bd4\u5982 JavaScript \u91cc\u5e76\u6ca1\u6709\u7c7b\u7684\u6982\u5ff5\uff0c\u4f46\u662f\u76ee\u524d\u6d4f\u89c8\u5668\u7684 JavaScript \u662f ES5 \u7248\u672c\uff0c\u5927\u591a\u6570\u9ad8\u7248\u672c\u7684\u6d4f\u89c8\u5668\u4e5f\u652f\u6301 ES6\uff0c\u4e0d\u8fc7\u53ea\u5b9e\u73b0\u4e86 ES6 \u7684\u90e8\u5206\u7279\u6027\u548c\u529f\u80fd\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u6982\u8ff0<\/strong><\/div>\n

\u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n

\u4ece\u8bed\u6cd5\u4e0a\u8bf4\uff0cPromise \u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ece\u5b83\u53ef\u4ee5\u83b7\u53d6\u5f02\u6b65\u64cd\u4f5c\u7684\u6d88\u606f\u3002<\/p>\n

Promise \u72b6\u6001<\/strong><\/div>\n
\u72b6\u6001\u7684\u7279\u70b9<\/strong><\/span><\/div>\n

Promise \u5f02\u6b65\u64cd\u4f5c\u6709\u4e09\u79cd\u72b6\u6001\uff1apending\uff08\u8fdb\u884c\u4e2d\uff09\u3001fulfilled\uff08\u5df2\u6210\u529f\uff09\u548c rejected\uff08\u5df2\u5931\u8d25\uff09\u3002\u9664\u4e86\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c\uff0c\u4efb\u4f55\u5176\u4ed6\u64cd\u4f5c\u90fd\u65e0\u6cd5\u6539\u53d8\u8fd9\u4e2a\u72b6\u6001\u3002<\/p>\n

Promise \u5bf9\u8c61\u53ea\u6709\uff1a\u4ece pending \u53d8\u4e3a fulfilled \u548c\u4ece pending \u53d8\u4e3a rejected \u7684\u72b6\u6001\u6539\u53d8\u3002\u53ea\u8981\u5904\u4e8e fulfilled \u548c rejected \uff0c\u72b6\u6001\u5c31\u4e0d\u4f1a\u518d\u53d8\u4e86\u5373 resolved\uff08\u5df2\u5b9a\u578b\uff09\u3002<\/p>\n

const p1 = new Promise(function(resolve,reject){\r\n    resolve('success1');\r\n    resolve('success2');\r\n}); \r\nconst p2 = new Promise(function(resolve,reject){  \r\n    resolve('success3'); \r\n    reject('reject');\r\n});\r\np1.then(function(value){  \r\n    console.log(value); \/\/ success1\r\n});\r\np2.then(function(value){ \r\n    console.log(value); \/\/ success3\r\n});<\/pre>\n
\u72b6\u6001\u7684\u7f3a\u70b9<\/strong><\/span><\/div>\n

\u65e0\u6cd5\u53d6\u6d88 Promise \uff0c\u4e00\u65e6\u65b0\u5efa\u5b83\u5c31\u4f1a\u7acb\u5373\u6267\u884c\uff0c\u65e0\u6cd5\u4e2d\u9014\u53d6\u6d88\u3002<\/p>\n

\u5982\u679c\u4e0d\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\uff0cPromise \u5185\u90e8\u629b\u51fa\u7684\u9519\u8bef\uff0c\u4e0d\u4f1a\u53cd\u5e94\u5230\u5916\u90e8\u3002<\/p>\n

\u5f53\u5904\u4e8e pending \u72b6\u6001\u65f6\uff0c\u65e0\u6cd5\u5f97\u77e5\u76ee\u524d\u8fdb\u5c55\u5230\u54ea\u4e00\u4e2a\u9636\u6bb5\uff08\u521a\u521a\u5f00\u59cb\u8fd8\u662f\u5373\u5c06\u5b8c\u6210\uff09\u3002<\/p>\n

then \u65b9\u6cd5<\/strong><\/div>\n

then \u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f Promise \u6267\u884c\u6210\u529f\u65f6\u7684\u56de\u8c03\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f Promise \u6267\u884c\u5931\u8d25\u65f6\u7684\u56de\u8c03\uff0c\u4e24\u4e2a\u51fd\u6570\u53ea\u4f1a\u6709\u4e00\u4e2a\u88ab\u8c03\u7528\u3002<\/p>\n

then \u65b9\u6cd5\u7684\u7279\u70b9<\/strong><\/span><\/div>\n

\u5728 JavaScript \u4e8b\u4ef6\u961f\u5217\u7684\u5f53\u524d\u8fd0\u884c\u5b8c\u6210\u4e4b\u524d\uff0c\u56de\u8c03\u51fd\u6570\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n

const p = new Promise(function(resolve,reject){\r\n  resolve('success');\r\n});\r\n \r\np.then(function(value){\r\n  console.log(value);\r\n});\r\n \r\nconsole.log('first');\r\n\/\/ first\r\n\/\/ success<\/pre>\n

\u901a\u8fc7 .then \u5f62\u5f0f\u6dfb\u52a0\u7684\u56de\u8c03\u51fd\u6570\uff0c\u4e0d\u8bba\u4ec0\u4e48\u65f6\u5019\uff0c\u90fd\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n

\u901a\u8fc7\u591a\u6b21\u8c03\u7528<\/p>\n

.then<\/pre>\n

\uff0c\u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5b83\u4eec\u4f1a\u6309\u7167\u63d2\u5165\u987a\u5e8f\u5e76\u4e14\u72ec\u7acb\u8fd0\u884c\u3002<\/p>\n

const p = new Promise(function(resolve,reject){\r\n  resolve(1);\r\n}).then(function(value){ \/\/ \u7b2c\u4e00\u4e2athen \/\/ 1\r\n  console.log(value);\r\n  return value * 2;\r\n}).then(function(value){ \/\/ \u7b2c\u4e8c\u4e2athen \/\/ 2\r\n  console.log(value);\r\n}).then(function(value){ \/\/ \u7b2c\u4e09\u4e2athen \/\/ undefined\r\n  console.log(value);\r\n  return Promise.resolve('resolve'); \r\n}).then(function(value){ \/\/ \u7b2c\u56db\u4e2athen \/\/ resolve\r\n  console.log(value);\r\n  return Promise.reject('reject'); \r\n}).then(function(value){ \/\/ \u7b2c\u4e94\u4e2athen \/\/reject:reject\r\n  console.log('resolve:' + value);\r\n}, function(err) {\r\n  console.log('reject:' + err);\r\n});<\/pre>\n

then \u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a resolved \u6216 rejected \u72b6\u6001\u7684 Promise \u5bf9\u8c61\u7528\u4e8e\u94fe\u5f0f\u8c03\u7528\uff0c\u4e14 Promise \u5bf9\u8c61\u7684\u503c\u5c31\u662f\u8fd9\u4e2a\u8fd4\u56de\u503c\u3002<\/p>\n

then \u65b9\u6cd5\u6ce8\u610f\u70b9<\/strong><\/span><\/div>\n

\u7b80\u4fbf\u7684 Promise \u94fe\u5f0f\u7f16\u7a0b\u6700\u597d\u4fdd\u6301\u6241\u5e73\u5316\uff0c\u4e0d\u8981\u5d4c\u5957 Promise\u3002<\/p>\n

\u6ce8\u610f\u603b\u662f\u8fd4\u56de\u6216\u7ec8\u6b62 Promise \u94fe\u3002<\/p>\n

const p1 = new Promise(function(resolve,reject){\r\n  resolve(1);\r\n}).then(function(result) {\r\n  p2(result).then(newResult => p3(newResult));\r\n}).then(() => p4());<\/pre>\n

\u521b\u5efa\u65b0 Promise \u4f46\u5fd8\u8bb0\u8fd4\u56de\u5b83\u65f6\uff0c\u5bf9\u5e94\u94fe\u6761\u88ab\u6253\u7834\uff0c\u5bfc\u81f4 p4 \u4f1a\u4e0e p2 \u548c p3 \u540c\u65f6\u8fdb\u884c\u3002<\/p>\n

\u5927\u591a\u6570\u6d4f\u89c8\u5668\u4e2d\u4e0d\u80fd\u7ec8\u6b62\u7684 Promise \u94fe\u91cc\u7684 rejection\uff0c\u5efa\u8bae\u540e\u9762\u90fd\u8ddf\u4e0a .catch(error => console.log(error));<\/p>\n","protected":false},"excerpt":{"rendered":"

\u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u3002 \u4ece\u8bed\u6cd5\u4e0a\u8bf4\uff0cPromise \u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ece\u5b83\u53ef\u4ee5\u83b7\u53d6\u5f02\u6b65\u64cd\u4f5c\u7684\u6d88\u606f\u3002 Promis […]<\/p>\n","protected":false},"author":321,"featured_media":191306,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-191304","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=191304"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191304\/revisions"}],"predecessor-version":[{"id":191308,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191304\/revisions\/191308"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/191306"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=191304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=191304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=191304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}