{"id":1935,"date":"2023-03-25T14:26:38","date_gmt":"2023-03-25T06:26:38","guid":{"rendered":""},"modified":"2023-03-25T14:26:38","modified_gmt":"2023-03-25T06:26:38","slug":"ES6 Promises","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1935.html","title":{"rendered":"ES6 Promises"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

ES6 Promises<\/h1>\n<\/p><\/div>\n

ES6 Promises<\/h2>\n
\n Promise \u4ee3\u8868\u6700\u7ec8\u5b9e\u73b0\u7684\u4e1c\u897f\u3002 Promise \u53ef\u4ee5\u6839\u636e\u64cd\u4f5c\u7ed3\u679c\u88ab\u62d2\u7edd\u6216\u89e3\u51b3\u3002\n <\/div>\n
\n ES6 Promise \u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5728 JavaScript \u4e2d\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\u3002\u5f02\u6b65\u7f16\u7a0b\u5305\u62ec\u4ece\u4e3b\u7ebf\u7a0b\u5355\u72ec\u8fd0\u884c\u8fdb\u7a0b\uff0c\u5e76\u5728\u5b8c\u6210\u65f6\u901a\u77e5\u4e3b\u7ebf\u7a0b\u3002\u5728 Promises \u4e4b\u524d\uff0c\u56de\u8c03\u7528\u4e8e\u6267\u884c\u5f02\u6b65\u7f16\u7a0b\u3002\n <\/div>\n

\u56de\u8c03<\/h2>\n
\n \u56de\u8c03\u662f\u5904\u7406\u51fd\u6570\u6267\u884c\u7684\u4e00\u79cd\u65b9\u5f0f\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u6267\u884c\u5b8c\u6210\u4e4b\u540e\u3002\n <\/div>\n
\n \u56de\u8c03\u5c06\u6709\u52a9\u4e8e\u5904\u7406\u4e8b\u4ef6\u3002\u5728\u56de\u8c03\u4e2d\uff0c\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\n <\/div>\n

\u4e3a\u4ec0\u4e48\u9700\u8981 Promise\uff1f<\/h2>\n
\n \u56de\u8c03\u662f\u5904\u7406\u57fa\u672c\u60c5\u51b5\u7684\u597d\u65b9\u6cd5\uff0c\u4f8b\u5982\u6700\u5c11\u7684\u5f02\u6b65\u64cd\u4f5c\u3002\u4f46\u662f\u5f53\u4f60\u5f00\u53d1\u4e00\u4e2a\u5305\u542b\u5927\u91cf\u4ee3\u7801\u7684 Web \u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u4f7f\u7528 Callback \u4f1a\u5f88\u9ebb\u70e6\u3002\u8fd9\u79cd\u8fc7\u591a\u7684\u56de\u8c03\u5d4c\u5957\u901a\u5e38\u88ab\u79f0\u4e3a\u56de\u8c03\u5730\u72f1\u3002\n <\/div>\n
\n \u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528Promises\u800c\u4e0d\u662fCallbacks .\n <\/div>\n

Promise \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/h2>\n
\n Promise \u8868\u793a\u5f02\u6b65\u64cd\u4f5c\u7684\u5b8c\u6210\u3002\u5b83\u6839\u636e\u88ab\u62d2\u7edd\u6216\u5df2\u89e3\u51b3\u7684\u64cd\u4f5c\u8fd4\u56de\u4e00\u4e2a\u503c\u3002 Promise\u4e3b\u8981\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\uff0c\u5982\u4e0b\u56fe\u6240\u793a:\n <\/div>\n

ES6 Promises_https:\/\/bianchenghao6.com_\u3010ES6 \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n Pending- \u662f\u6bcf\u4e2aPromise\u7684\u521d\u59cb\u72b6\u6001\u3002\u8868\u793a\u7ed3\u679c\u8fd8\u6ca1\u6709\u8ba1\u7b97\u51fa\u6765\u3002\n <\/div>\n
\n Fulfilled- \u8868\u793a\u64cd\u4f5c\u5df2\u7ecf\u5b8c\u6210\u3002\n <\/div>\n
\n Rejected- > \u5b83\u4ee3\u8868\u5728\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u5931\u8d25\u3002\n <\/div>\n
\n \u4e00\u65e6 Promise \u88ab\u5b9e\u73b0\u6216\u62d2\u7edd\uff0c\u5b83\u5c06\u662f\u4e0d\u53ef\u53d8\u7684\u3002 Promise() \u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f rejected \u51fd\u6570\u548c resolve \u51fd\u6570\u3002\u57fa\u4e8e\u5f02\u6b65\u64cd\u4f5c\uff0c\u5b83\u8fd4\u56de\u7b2c\u4e00\u4e2a\u53c2\u6570\u6216\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\n <\/div>\n

\u521b\u5efa\u4e00\u4e2a Promise<\/h2>\n
\n \u5728 JavaScript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 Promise() \u6784\u9020\u51fd\u6570\u3002\n <\/div>\n
\n \u8bed\u6cd5\n <\/div>\n
\n
\nconst Promise = new <\/span>Promise((resolve,reject) => {....});<\/pre>\n

<\/code>\n <\/div>\n

\n \u793a\u4f8b\n <\/div>\n
\n
\nlet Promise = new <\/span>Promise((resolve, reject)=>{\n    let a = 3;\n    if(a==3){\n        resolve('Success'<\/span>);\n    }\n    else{\n        reject('Failed'<\/span>);\n    }\n})\nPromise.then<\/span>((message)=>{\n    console.log<\/span>(\"It is then block. The message is: ?+ message)\n}).catch<\/span>((message)=>{\nconsole.log<\/span>(\"It is catch <\/span>block. The message is: ?+ message)\n})<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
It is then block. The message is: Success<\/pre>\n

<\/code>\n <\/div>\n

Promise \u65b9\u6cd5<\/h2>\n
\n Promise \u65b9\u6cd5\u7528\u4e8e\u5904\u7406 Promise \u5bf9\u8c61\u7684\u62d2\u7edd\u6216\u89e3\u6790\u3002\u8ba9\u6211\u4eec\u4e86\u89e3\u4e00\u4e0b Promise \u65b9\u6cd5\u7684\u7b80\u8981\u8bf4\u660e\u3002\n <\/div>\n

.then()<\/h3>\n
\n \u5f53 Promise \u5b8c\u6210\u6216\u62d2\u7edd\u65f6\u8c03\u7528\u6b64\u65b9\u6cd5\u3002\u53ef\u4ee5\u94fe\u63a5\u6b64\u65b9\u6cd5\u6765\u5904\u7406 Promise \u7684\u62d2\u7edd\u6216\u5c65\u884c\u3002 resolved \u548c rejected \u9700\u8981\u4e24\u4e2a\u51fd\u6570\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u5728 Promise \u5b8c\u6210\u65f6\u88ab\u8c03\u7528\uff0c\u7b2c\u4e8c\u4e2a(\u53ef\u9009)\u5728 Promise \u88ab\u62d2\u7edd\u65f6\u88ab\u8c03\u7528\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u6765\u7406\u89e3\u5982\u4f55\u5904\u7406 Promise \u62d2\u7edd\u548c\u89e3\u6790\u4f7f\u7528 .then() \u65b9\u6cd5\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
\nlet success = (a) => {\n    console.log<\/span>(a + \" it worked!\"<\/span>)\n  }\n  \n  let error = (a) => {\n    console.log<\/span>(a + \" it failed!\"<\/span>)\n  }\n  \n  const Promise = num => {\n    return new <\/span>Promise((resolve,reject) => {\n      if((num%2)==0){\n        resolve(\"Success!\"<\/span>)\n      }\n      reject(\"Failure!\"<\/span>)\n    })\n  }\n  \n  Promise(100).then<\/span>(success, error) \n  Promise(21).then<\/span>(success,error)<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
Success! it worked!\nFailure! it failed!<\/pre>\n

<\/code>\n <\/div>\n

.catch()<\/h3>\n
\n \u8fd9\u662f\u5904\u7406\u5931\u8d25\u548c\u62d2\u7edd\u7684\u597d\u65b9\u6cd5\u3002\u5b83\u53ea\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u53c2\u6570\u6765\u5904\u7406\u9519\u8bef\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u6765\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 .catch() \u65b9\u6cd5\u6765\u5904\u7406 Promise \u62d2\u7edd\u548c\u5931\u8d25\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
\nconst Promise = num => {\n    return new <\/span>Promise((resolve,reject) => {\n      if(num > 0){\n        resolve(\"Success!\"<\/span>)\n      }\n      reject(\"Failure!\"<\/span>)\n    })\n  }\n  \n  Promise(20).then<\/span>(res => {\n    throw new <\/span>Error();\n    console.log<\/span>(res + \" success!\"<\/span>)\n  }).catch<\/span>(error => {\n    console.log<\/span>(error + \" oh no, it failed!\"<\/span>)\n  })<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
Error oh no, it failed!<\/pre>\n

<\/code>\n <\/div>\n

.resolve()<\/h3>\n
\n \u5b83\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Promise \u5bf9\u8c61\uff0c\u5b83\u7528\u7ed9\u5b9a\u7684\u503c\u89e3\u6790\u3002\u5982\u679c\u8be5\u503c\u5177\u6709 .then() \u65b9\u6cd5\uff0c\u5219\u8fd4\u56de\u7684 Promise \u5c06\u9075\u5faa .then() \u65b9\u6cd5\u91c7\u7528\u5176\u6700\u7ec8\u72b6\u6001\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u7684 Promise \u5c06\u4f7f\u7528 value \u5b9e\u73b0\u3002\n <\/div>\n
\n
\nPromise.resolve<\/span>('Success'<\/span>).then<\/span>(function(val) {\n    console.log<\/span>(val);\n  }, function(val) {\n  });<\/pre>\n

<\/code>\n <\/div>\n

.reject()<\/h3>\n
\n \u5b83\u8fd4\u56de\u4e00\u4e2a\u88ab\u62d2\u7edd\u7684\u5177\u6709\u7ed9\u5b9a\u503c\u7684 Promise \u5bf9\u8c61\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
\nfunction resolved(result) {\n    console.log<\/span>('Resolved'<\/span>);\n  }\n  \n  function rejected(result) {\n    console.error<\/span>(result);\n  }\n  \n  Promise.reject<\/span>(new Error('fail'<\/span>)).then<\/span>(resolved, rejected);<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
Error: fail<\/pre>\n

<\/code>\n <\/div>\n

.all()<\/h3>\n
\n \u5b83\u9700\u8981\u4e00\u4e2a Promise \u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u3002\u6b64\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a resolved Promise\uff0c\u5f53\u4f5c\u4e3a\u8fed\u4ee3\u4f20\u9012\u7684\u6240\u6709 Promise \u90fd\u5df2\u5b8c\u6210\u65f6\uff0c\u8be5 Promise \u4f1a\u5b8c\u6210\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
\nconst PromiseA = Promise.resolve<\/span>('Hello'<\/span>);\nconst PromiseB = 'World'<\/span>;\nconst PromiseC = new <\/span>Promise(function(resolve, reject) {\n  setTimeout(resolve, 100, 1000);\n});\nPromise.all<\/span>([PromiseA, PromiseB, PromiseC]).then<\/span>(function(values) {\n  console.log<\/span>(values);\n});<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
[ 'Hello'<\/span>, 'World'<\/span>, 1000 ]<\/pre>\n

<\/code>\n <\/div>\n

.race()<\/h3>\n
\n \u6b64\u65b9\u6cd5\u7528\u4e8e\u6839\u636e\u7b2c\u4e00\u4e2a\u89e3\u6790\u7684\u5f15\u7528\u7684 Promise \u8fd4\u56de\u5df2\u89e3\u6790\u7684 Promise\u3002\n <\/div>\n
\n \u793a\u4f8b\n <\/div>\n
\n
\nconst Promise1 = new <\/span>Promise((resolve,reject) => {\n    setTimeout(resolve(\"Promise 1 is first\"<\/span>),1000)\n})\nconst Promise2= new <\/span>Promise((resolve,reject) =>{\n    setTimeout(resolve(\"Promise 2 is first\"<\/span>),2000)\n})\nPromise.race<\/span>([Promise1,Promise2]).then<\/span>(result => {\n    console.log<\/span>(result);\n})<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa\n <\/div>\n
\n
Promise 1 is first<\/pre>\n

<\/code>\n <\/div>\n

ES6 Promise Chaining<\/h2>\n
\n Promise \u94fe\u5141\u8bb8\u6211\u4eec\u63a7\u5236 JavaScript \u5f02\u6b65\u64cd\u4f5c\u7684\u6d41\u7a0b\u3002\u901a\u8fc7\u4f7f\u7528 Promise \u94fe\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Promise \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u8f93\u5165\u3002\n <\/div>\n

ES6 Promises_https:\/\/bianchenghao6.com_\u3010ES6 \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u6709\u65f6\uff0c\u5e0c\u671b\u5c06 Promise \u94fe\u63a5\u5728\u4e00\u8d77\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u51e0\u4e2a\u5f02\u6b65\u64cd\u4f5c\u8981\u6267\u884c\u3002\u5f53\u4e00\u4e2a\u64cd\u4f5c\u7ed9\u6211\u4eec\u6570\u636e\u65f6\uff0c\u6211\u4eec\u5c06\u5f00\u59cb\u5bf9\u90a3\u5757\u6570\u636e\u8fdb\u884c\u53e6\u4e00\u4e2a\u64cd\u4f5c\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002\n <\/div>\n
\n \u5f53\u6211\u4eec\u6709\u591a\u4e2a\u76f8\u4e92\u4f9d\u8d56\u7684\u5f02\u6b65\u51fd\u6570\u65f6\uff0cPromise \u94fe\u5f88\u6709\u7528\uff0c\u5e76\u4e14\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u5e94\u8be5\u5728\u4e4b\u540e\u8fd0\u884c\u4e00\u4e2a\u53e6\u4e00\u4e2a\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u5b50\u6765\u5c1d\u8bd5\u7406\u89e3 Promise \u94fe\u7684\u6982\u5ff5:\n <\/div>\n
\n \u4f8b\u5b50\n <\/div>\n
\n
\n<!DOCTYPE html>\n<html lang=\"en\"<\/span>>\n<head>\n    <meta charset=\"UTF-8\"<\/span>>\n    <meta name=\"viewport\"<\/span> content=\"width=device-width, initial-scale=1.0\"<\/span>>\n    <meta http-equiv=\"X-UA-Compatible\"<\/span> content=\"ie=edge\"<\/span>>\n    \n    <title>Document<\/title>\n<\/head>\n<body>\n    <script>\n        const PromiseA = () =>{\n            return new <\/span>Promise((resolve,reject)=>{\n                resolve(\"Hello Promoise A\"<\/span>);\n            });\n        }\n \n        const PromiseB = () =>{\n            return new <\/span>Promise((resolve,reject)=>{\n                resolve(\"Hello Promise B\"<\/span>);\n            });\n        }\n        const PromiseC = () =>{\n            return new <\/span>Promise((resolve,reject)=>{\n                resolve(\"Hello Promise C\"<\/span>);\n            });\n        }\n        PromiseA().then<\/span>((A)=>{\n                  console.log<\/span>(A);\n                    return <\/span>PromiseB();\n                }).then<\/span>((B)=>{\n                    console.log<\/span>(B);\n                    return <\/span>PromiseC();\n                }).then<\/span>((C)=>{\n                    console.log<\/span>(C);\n                });       \n     <\/script>\n<\/body>\n<\/html><\/pre>\n

<\/code>\n <\/div>\n

\n \u5728\u6d4f\u89c8\u5668\u4e2d\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u4f7f\u7528ctrl+shift+I\u6253\u5f00\u7ec8\u7aef\u3002\u4e0a\u9762\u4ee3\u7801\u6267\u884c\u6210\u529f\u540e\uff0c\u6211\u4eec\u4f1a\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\u3002\n <\/div>\n
\n \u8f93\u51fa\n <\/div>\n

ES6 Promises_https:\/\/bianchenghao6.com_\u3010ES6 \u6559\u7a0b\u3011_\u7b2c3\u5f20
\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"ES6 Promiseszh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[161],"tags":[],"class_list":["post-1935","post","type-post","status-publish","format-standard","hentry","category-es6jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1935"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1935"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1935\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}