{"id":3856,"date":"2021-08-31T15:42:05","date_gmt":"2021-08-31T07:42:05","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3856"},"modified":"2021-08-31T15:42:05","modified_gmt":"2021-08-31T07:42:05","slug":"promise-%e7%9a%84%e9%93%be%e5%bc%8f%e8%b0%83%e7%94%a8%e4%b8%8e%e4%b8%ad%e6%ad%a2","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3856","title":{"rendered":"Promise \u7684\u94fe\u5f0f\u8c03\u7528\u4e0e\u4e2d\u6b62"},"content":{"rendered":"\n<p>\u672c\u6587\u4e3b\u8981\u8bb2\u7684\u662f\u5982\u4f55\u5b9e\u73b0 Promise \u7684\u94fe\u5f0f\u8c03\u7528\u3002\u4e5f\u5c31\u662f promise().then().then().catch() \u7684\u5f62\u5f0f\uff0c\u7136\u540e\u8ba8\u8bba\u5982\u4f55\u5728\u67d0\u4e00\u4e2a then() \u91cc\u9762\u4e2d\u6b62 Promise\u3002<\/p>\n\n\n\n<p>\u5728\u7a0b\u5e8f\u4e2d\uff0c\u53ea\u8981\u8fd4\u56de\u4e86\u4e00\u4e2a promise \u5bf9\u8c61\uff0c\u5982\u679c promise \u5bf9\u8c61\u4e0d\u662f Rejected \u6216 Fulfilled \u72b6\u6001\uff0cthen \u65b9\u6cd5\u5c31\u4f1a\u7ee7\u7eed\u8c03\u7528\u3002\u5229\u7528\u8fd9\u4e2a\u7279\u6027\uff0c\u53ef\u4ee5\u5904\u7406\u591a\u4e2a\u5f02\u6b65\u903b\u8f91\u3002\u4f46\u6709\u65f6\u5019\u67d0\u4e2a then \u65b9\u6cd5\u7684\u6267\u884c\u7ed3\u679c\u53ef\u80fd\u4f1a\u51b3\u5b9a\u662f\u5426\u9700\u8981\u6267\u884c\u4e0b\u4e00\u4e2a then\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u9700\u4e2d\u6b62 promise\uff0c\u4e3b\u8981\u601d\u60f3\u5c31\u662f\u4f7f\u7528 reject \u6765\u4e2d\u6b62 promise \u7684 then \u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u201c\u4e2d\u6b62\u201d\u8fd9\u4e2a\u8bcd\u4e0d\u77e5\u9053\u7528\u5f97\u662f\u5426\u51c6\u786e\u3002\u8fd9\u91cc\u53ef\u80fd\u8fd8\u662f break \u7684\u542b\u4e49\u66f4\u7cbe\u786e\uff0c\u8df3\u51fa\u672c\u6b21 promise\uff0c\u4e0d\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684 then \u65b9\u6cd5\u3002\u4f46 promise \u4f9d\u65e7\u4f1a\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p><strong>Can I use promises<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/08\/image-1024x423.png\" alt=\"\" class=\"wp-image-3857\" width=\"608\" height=\"250\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-1024x423.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-300x124.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-768x317.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-830x343.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-230x95.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-350x145.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-480x198.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image.png 1384w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/figure><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">Promise<\/h5>\n\n\n\n<p>\u5148\u7b80\u5355\u590d\u4e60\u4e00\u4e0b Promise\u3002Promise \u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u4e00\u4e2a\u5904\u7406\u5f02\u6b65\u7684\u65b9\u6cd5\u3002\u4e00\u822c\u53ef\u4ee5\u901a\u8fc7 new \u65b9\u6cd5\u6765\u8c03\u7528 Promise \u7684\u6784\u9020\u5668\u5b9e\u4f8b\u5316\u4e00\u4e2a promise \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var promise = new Promise((resolve, reject) => {\n    \/\/ \u5f02\u6b65\u5904\u7406\n    \/\/ \u5904\u7406\u7ed3\u675f\u540e\uff0c\u8c03\u7528 resolve \u6216 reject\n    \/\/      \u6210\u529f\u65f6\u5c31\u8c03\u7528 resolve\n    \/\/      \u5931\u8d25\u65f6\u5c31\u8c03\u7528 reject\n});\n<\/pre>\n\n\n\n<p>\u7528 new Promise \u5b9e\u4f8b\u5316\u7684 promise \u5bf9\u8c61\u6709\u4ee5\u4e0b\u4e09\u4e2a\u72b6\u6001\uff1a<\/p>\n\n\n\n<ul><li>\u201chas-resolution\u201d &#8211; Fulfilled\u3002resolve(\u6210\u529f)\u65f6\uff0c\u6b64\u65f6\u4f1a\u8c03\u7528 onFulfilled<\/li><li>\u201chas-rejection\u201d &#8211; Rejected\u3002reject(\u5931\u8d25)\u65f6\uff0c\u6b64\u65f6\u4f1a\u8c03\u7528 onRejected<\/li><li>\u201cunresolved\u201d &#8211; Pending\u3002\u65e2\u4e0d\u662fresolve\u4e5f\u4e0d\u662freject\u7684\u72b6\u6001\uff0c\u4e5f\u5c31\u662fpromise\u5bf9\u8c61\u521a\u88ab\u521b\u5efa\u540e\u7684\u521d\u59cb\u5316\u72b6\u6001<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/08\/image-1.png\" alt=\"\" class=\"wp-image-3858\" width=\"402\" height=\"226\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-1.png 634w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-1-300x168.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-1-230x129.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-1-350x197.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-1-480x270.png 480w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><\/figure><\/div>\n\n\n\n<p>\u5173\u4e8e\u4e0a\u9762\u8fd9\u4e09\u79cd\u72b6\u6001\u7684\u8bfb\u6cd5\uff0c\u5176\u4e2d\u5de6\u4fa7\u4e3a\u5728 ES6 Promises \u89c4\u8303\u4e2d\u5b9a\u4e49\u7684\u672f\u8bed\uff0c \u800c\u53f3\u4fa7\u5219\u662f\u5728 Promises\/A+ \u4e2d\u63cf\u8ff0\u72b6\u6001\u7684\u672f\u8bed\u3002\u57fa\u672c\u4e0a\u72b6\u6001\u5728\u4ee3\u7801\u4e2d\u662f\u4e0d\u4f1a\u6d89\u53ca\u5230\u7684\uff0c\u6240\u4ee5\u540d\u79f0\u4e5f\u65e0\u9700\u592a\u5728\u610f\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Promise Chain<\/h5>\n\n\n\n<p>\u5148\u6765\u5047\u8bbe\u4e00\u4e2a\u4e1a\u52a1\u9700\u6c42\uff1a\u5728\u7cfb\u7edf\u4e2d\u4f7f\u7528\u6559\u52a1\u7cfb\u7edf\u8d26\u53f7\u8fdb\u884c\u767b\u5f55\u3002\u9996\u5148\u7528\u6237\u5728\u767b\u5f55\u9875\u9762\u8f93\u5165\u7528\u6237\u540d\uff08\u6559\u52a1\u7cfb\u7edf\u8d26\u53f7\uff09\u548c\u5bc6\u7801\uff08\u6559\u52a1\u7cfb\u7edf\u5bc6\u7801\uff09\uff1b\u7136\u540e\u5224\u65ad\u6570\u636e\u5e93\u4e2d\u662f\u5426\u5b58\u5728\u8be5\u7528\u6237\uff1b\u5982\u679c\u4e0d\u5b58\u5728\u5219\u4f7f\u7528\u7528\u6237\u540d\u548c\u5bc6\u7801\u6a21\u62df\u767b\u5f55\u6559\u52a1\u7cfb\u7edf\uff0c\u5982\u679c\u6a21\u62df\u767b\u5f55\u6210\u529f\uff0c\u5219\u5b58\u50a8\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5e76\u8fd4\u56de\u767b\u5f55\u6210\u529f\u3002<\/p>\n\n\n\n<p>\u542c\u8d77\u6765\u5c31\u6709\u70b9\u590d\u6742\u5bf9\u4e0d\u5bf9\uff1f\u4e8e\u662f\u753b\u4e86\u4e2a\u6d41\u7a0b\u56fe\u6765\u89e3\u91ca\u6574\u4e2a\u4e1a\u52a1\u903b\u8f91\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/08\/image-2-969x1024.png\" alt=\"\" class=\"wp-image-3860\" width=\"528\" height=\"556\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-2-284x300.png 284w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-2-230x243.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-2-350x370.png 350w\" sizes=\"(max-width: 528px) 100vw, 528px\" \/><\/figure><\/div>\n\n\n\n<p>\u4e0a\u56fe\u53ea\u662f\u4e00\u4e2a\u7b80\u5316\u7248\u672c\uff0c\u6bd4\u5982\u5bc6\u7801\u52a0\u5bc6\u3001session\u8bbe\u7f6e\u7b49\u6ca1\u6709\u8868\u73b0\u51fa\u6765\uff0c\u5927\u5bb6\u77e5\u9053\u5c31\u597d\u3002\u56fe\u4e2d (1)\u3001(2)\u3001(3) \u4e09\u4e2a\u5730\u65b9\u5c31\u662f\u4f1a\u8fdb\u884c\u5f02\u6b65\u5904\u7406\u7684\u5730\u65b9\uff0c\u4e00\u822c\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u7f51\u7edc\u8bf7\u6c42\u90fd\u662f\u5f02\u6b65\u7684\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u7528\u4f20\u7edf\u7684\u56de\u8c03\u51fd\u6570 callback \u6765\u5904\u7406\u4e0a\u9762\u7684\u903b\u8f91\uff0c\u5d4c\u5957\u7684\u5c42\u7ea7\u5c31\u4f1a\u6bd4\u8f83\u6df1\uff0c\u4e0a\u9762\u7684\u4e1a\u52a1\u56e0\u4e3a\u6709\u4e09\u4e2a\u5f02\u6b65\u64cd\u4f5c\u6240\u4ee5\u6709\u4e09\u5c42\u56de\u8c03\uff0c\u4ee3\u7801\u5927\u6982\u4f1a\u662f\u4e0b\u9762\u7684\u6837\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u6839\u636e name \u67e5\u8be2\u7528\u6237\u4fe1\u606f\nfindUserByName(name, function(err, userinfo) {\n  if (err) {\n    return res.json({\n      code: 1000,\n      message: '\u67e5\u8be2\u7528\u6237\u4fe1\u606f\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u6570\u51fa\u73b0\u5f02\u5e38',\n    });\n  }\n\n\n  if (userinfo.length > 0) {\n  \/\/ \u7528\u6237\u5b58\u5728\n  if (userinfo[0].pwd === pwd)\n    \/\/ \u5bc6\u7801\u6b63\u786e\n    return res.json({\n      code: 0,\n      message: '\u767b\u5f55\u6210\u529f',\n    });\n  }\n\n  \/\/ \u6570\u636e\u5e93\u4e2d\u4e0d\u5b58\u5728\u8be5\u7528\u6237\uff0c\u6a21\u62df\u767b\u5f55\u6559\u52a1\u7cfb\u7edf\n  loginEducationSystem(name, pwd, function(err, result) {\n    if (err) {\n      return res.json({\n        code: 1001,\n        message: '\u6a21\u62df\u767b\u5f55\u6559\u52a1\u7cfb\u7edf\u51fa\u73b0\u5f02\u5e38',\n      });\n    }\n\n    \/\/ \u7ea6\u5b9a\u6b63\u786e\u60c5\u51b5\u4e0b\uff0ccode \u4e3a 0\n    if (result.code !== 0) {\n      return res.json({\n        code: 1002,\n        message: '\u6a21\u62df\u767b\u5f55\u6559\u52a1\u7cfb\u7edf\u5931\u8d25\uff0c\u53ef\u80fd\u662f\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef',\n      });\n    }\n\n    \/\/ \u6a21\u62df\u767b\u5f55\u6210\u529f\uff0c\u5c06\u7528\u6237\u540d\u5bc6\u7801\u5b58\u5165\u6570\u636e\u5e93\n    saveUserToDB(name, pwd, function(err, result) {\n      if (err) {\n        return res.json({\n          code: 1003,\n          message: '\u5c06\u7528\u6237\u540d\u5bc6\u7801\u5b58\u5165\u6570\u636e\u5e93\u51fa\u73b0\u5f02\u5e38',\n        });\n      }\n      if (result.code !== 0) {\n        return res.json({\n          code: 1004,\n          message: '\u5c06\u7528\u6237\u540d\u5bc6\u7801\u5b58\u5165\u6570\u636e\u5e93\u51fa\u73b0\u5f02\u5e38',\n        });\n      }\n\n      return res.json({\n        code: 0,\n        message: '\u767b\u5f55\u6210\u529f!',\n      });\n    });\n  });\n});<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u5b58\u5728\u7684\u4e0d\u4f18\u96c5\u4e4b\u5904\uff1a<\/p>\n\n\n\n<ul><li>\u968f\u7740\u4e1a\u52a1\u903b\u8f91\u53d8\u8d1f\u8d23\uff0c\u56de\u8c03\u5c42\u7ea7\u4f1a\u8d8a\u6765\u8d8a\u6df1<\/li><li>\u4ee3\u7801\u8026\u5408\u5ea6\u6bd4\u8f83\u9ad8\uff0c\u4e0d\u6613\u4fee\u6539<\/li><li>\u6bcf\u4e00\u6b65\u64cd\u4f5c\u90fd\u9700\u8981\u624b\u52a8\u8fdb\u884c\u5f02\u5e38\u5904\u7406\uff0c\u6bd4\u8f83\u9ebb\u70e6<\/li><\/ul>\n\n\n\n<p>\u63a5\u4e0b\u6765\u518d\u7528 promise \u5b9e\u73b0\u6b64\u5904\u7684\u4e1a\u52a1\u9700\u6c42\u3002\u4f7f\u7528 promise \u7f16\u7801\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u601d\u8003\u4e24\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u4e00\u662f\u5982\u4f55\u94fe\u5f0f\u8c03\u7528\uff0c\u4e8c\u662f\u5982\u4f55\u4e2d\u6b62\u94fe\u5f0f\u8c03\u7528\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">How to Use Promise Chain<\/h5>\n\n\n\n<p>\u4e1a\u52a1\u4e2d\u6709\u4e09\u4e2a\u9700\u8981\u5f02\u6b65\u5904\u7406\u7684\u529f\u80fd\uff0c\u6240\u4ee5\u4f1a\u5206\u522b\u5b9e\u4f8b\u5316\u4e09\u4e2a promise \u5bf9\u8c61\uff0c\u7136\u540e\u5bf9 promise \u8fdb\u884c\u94fe\u5f0f\u8c03\u7528\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u8fdb\u884c\u94fe\u5f0f\u8c03\u7528\uff1f<\/p>\n\n\n\n<p>\u5176\u5b9e\u4e5f\u5f88\u7b80\u5355\uff0c\u76f4\u63a5\u5728 promise \u7684 then \u65b9\u6cd5\u91cc\u9762\u8fd4\u56de\u53e6\u4e00\u4e2a promise \u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function start() {\n  return new Promise((resolve, reject) => {\n    resolve('start');\n  });\n}\n\nstart()\n  .then(data => {\n    \/\/ promise start\n    console.log('result of start: ', data);\n    return Promise.resolve(1); \/\/ p1\n  })\n  .then(data => {\n    \/\/ promise p1\n    console.log('result of p1: ', data);\n    return Promise.reject(2); \/\/ p2\n  })\n  .then(data => {\n    \/\/ promise p2\n    console.log('result of p2: ', data);\n    return Promise.resolve(3); \/\/ p3\n  })\n  .catch(ex => {\n    \/\/ promise p3\n    console.log('ex: ', ex);\n    return Promise.resolve(4); \/\/ p4\n  })\n  .then(data => {\n    \/\/ promise p4\n    console.log('result of p4: ', data);\n  });\n<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6700\u7ec8\u4f1a\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">result of start:  start\nresult of p1:  1\nex:  2\nresult of p4:  4\n<\/pre>\n\n\n\n<p>\u4ee3\u7801\u7684\u6267\u884c\u903b\u8f91\u5982\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/08\/image-3-887x1024.png\" alt=\"\" class=\"wp-image-3861\" width=\"462\" height=\"533\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-3-887x1024.png 887w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-3-260x300.png 260w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-3-768x886.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-3-830x958.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-3-230x265.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-3-350x404.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-3-480x554.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/image-3.png 1156w\" sizes=\"(max-width: 462px) 100vw, 462px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\u6765\uff0c\u4ee3\u7801\u7684\u6267\u884c\u903b\u8f91\u662f promise start &#8211;> promise p1 &#8211;> promise p3 &#8211;> promise p4\u3002\u6240\u4ee5\u7ed3\u5408\u8f93\u51fa\u7ed3\u679c\u548c\u6267\u884c\u903b\u8f91\u56fe\uff0c\u603b\u7ed3\u51fa\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ul><li>promise \u7684 then \u65b9\u6cd5\u91cc\u9762\u53ef\u4ee5\u7ee7\u7eed\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 promise \u5bf9\u8c61<\/li><li>\u4e0b\u4e00\u4e2a then \u65b9\u6cd5\u7684\u53c2\u6570\u662f\u4e0a\u4e00\u4e2a promise \u5bf9\u8c61\u7684 resolve \u53c2\u6570<\/li><li>catch \u65b9\u6cd5\u7684\u53c2\u6570\u662f\u5176\u4e4b\u524d\u67d0\u4e2a promise \u5bf9\u8c61\u7684 rejecte \u53c2\u6570<\/li><li>\u4e00\u65e6\u67d0\u4e2a then \u65b9\u6cd5\u91cc\u9762\u7684 promise \u72b6\u6001\u6539\u53d8\u4e3a\u4e86 rejected\uff0c\u5219promise \u65b9\u6cd5\u8fde\u4f1a\u8df3\u8fc7\u540e\u9762\u7684 then \u76f4\u63a5\u6267\u884c catch<\/li><li>catch \u65b9\u6cd5\u91cc\u9762\u4f9d\u65e7\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 promise \u5bf9\u8c61<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">How to Break Promise Chain<\/h5>\n\n\n\n<p>\u63a5\u4e0b\u6765\u5c31\u8be5\u8ba8\u8bba\u5982\u4f55\u4e2d\u6b62 promise \u65b9\u6cd5\u94fe\u4e86\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053 promise \u7684\u72b6\u6001\u6539\u53d8\u4e3a rejected \u540e\uff0cpromise \u5c31\u4f1a\u8df3\u8fc7\u540e\u9762\u7684 then \u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\uff0c\u67d0\u4e2a then \u91cc\u9762\u53d1\u751f\u5f02\u5e38\u540e\uff0c\u5c31\u4f1a\u8df3\u8fc7 then \u65b9\u6cd5\uff0c\u76f4\u63a5\u6267\u884c catch\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5f53\u5728\u6784\u9020\u7684 promise \u65b9\u6cd5\u94fe\u4e2d\uff0c\u5982\u679c\u5728\u67d0\u4e2a then \u540e\u9762\uff0c\u4e0d\u9700\u8981\u518d\u6267\u884c then \u65b9\u6cd5\u4e86\uff0c\u5c31\u53ef\u4ee5\u628a\u5b83\u5f53\u4f5c\u4e00\u4e2a\u5f02\u5e38\u6765\u5904\u7406\uff0c\u8fd4\u56de\u4e00\u4e2a\u5f02\u5e38\u4fe1\u606f\u7ed9 catch\uff0c\u5176\u53c2\u6570\u53ef\u81ea\u5b9a\u4e49\uff0c\u6bd4\u5982\u8be5\u5f02\u5e38\u7684\u53c2\u6570\u4fe1\u606f\u4e3a { notRealPromiseException: true}\uff0c\u7136\u540e\u5728 catch \u91cc\u9762\u5224\u65ad\u4e00\u4e0b notRealPromiseException \u662f\u5426\u4e3a true\uff0c\u5982\u679c\u4e3a true\uff0c\u5c31\u8bf4\u660e\u4e0d\u662f\u7a0b\u5e8f\u51fa\u73b0\u5f02\u5e38\uff0c\u800c\u662f\u5728\u6b63\u5e38\u903b\u8f91\u91cc\u9762\u4e2d\u6b62 then \u65b9\u6cd5\u7684\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u5927\u6982\u5c31\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">start()\n  .then(data => {\n    \/\/ promise start\n    console.log('result of start: ', data);\n    return Promise.resolve(1); \/\/ p1\n    )\n  .then(data => {\n    \/\/ promise p1\n    console.log('result of p1: ', data);\n    return Promise.reject({\n      notRealPromiseException: true,\n    }); \/\/ p2\n  })\n  .then(data => {\n    \/\/ promise p2\n    console.log('result of p2: ', data);\n    return Promise.resolve(3); \/\/ p3\n  })\n  .catch(ex => {\n    console.log('ex: ', ex);\n    if (ex.notRealPromiseException) {\n      \/\/ \u4e00\u5207\u6b63\u5e38\uff0c\u53ea\u662f\u901a\u8fc7 catch \u65b9\u6cd5\u6765\u4e2d\u6b62 promise chain\n      \/\/ \u4e5f\u5c31\u662f\u4e2d\u6b62 promise p2 \u7684\u6267\u884c\n      return true;\n    }\n    \/\/ \u771f\u6b63\u53d1\u751f\u5f02\u5e38\n    return false;\n  });\n<\/pre>\n\n\n\n<p>\u8fd9\u6837\u7684\u505a\u6cd5\u53ef\u80fd\u4e0d\u7b26\u5408 catch \u7684\u8bed\u4e49\u3002\u4e0d\u8fc7\u4ece\u67d0\u79cd\u610f\u4e49\u4e0a\u6765\u8bf4\uff0cpromise \u65b9\u6cd5\u94fe\u6ca1\u6709\u7ee7\u7eed\u6267\u884c\uff0c\u4e5f\u53ef\u4ee5\u7b97\u662f\u4e00\u79cd\u201c\u5f02\u5e38\u201d\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Refactor Callback with Promise<\/h5>\n\n\n\n<p>\u8bb2\u4e86\u90a3\u4e48\u591a\u9053\u7406\uff0c\u73b0\u5728\u5c31\u6539\u6765\u4f7f\u7528 promise \u91cd\u6784\u4e4b\u524d\u7528\u56de\u8c03\u51fd\u6570\u5199\u7684\u5f02\u6b65\u903b\u8f91\u4e86\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u636e name \u67e5\u8be2\u7528\u6237\u4fe1\u606f\nconst findUserByName = (name, pwd) => {\n  return new Promise((resolve, reject) => {\n    \/\/ \u6570\u636e\u5e93\u67e5\u8be2\u64cd\u4f5c\n    if (dbError) {\n      \/\/ \u6570\u636e\u5e93\u67e5\u8be2\u51fa\u9519\uff0c\u5c06 promise \u8bbe\u7f6e\u4e3a rejected\n      reject({\n        code: 1000,\n        message: '\u67e5\u8be2\u7528\u6237\u4fe1\u606f\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u6570\u51fa\u73b0\u5f02\u5e38',\n      });\n    }\n    \/\/ \u5c06\u67e5\u8be2\u7ed3\u679c\u8d4b\u7ed9 userinfo \u53d8\u91cf\n    if (userinfo.length === 0) {\n      \/\/ \u6570\u636e\u5e93\u4e2d\u4e0d\u5b58\u5728\u8be5\u7528\u6237\n      resolve();\n    }\n    \/\/ \u6570\u636e\u5e93\u5b58\u5728\u8be5\u7528\u6237\uff0c\u5224\u65ad\u5bc6\u7801\u662f\u5426\u6b63\u786e\n    if (pwd === userinfo[0].pwd) {\n      \/\/ \u5bc6\u7801\u6b63\u786e\uff0c\u4e2d\u6b62 promise \u6267\u884c\n      reject({\n        notRealPromiseException: true,\n        data: {\n          code: 0,\n          message: '\u5bc6\u7801\u6b63\u786e\uff0c\u767b\u5f55\u6210\u529f',\n        }\n      });\n    }\n    \/\/ \u5bc6\u7801\u4e0d\u6b63\u786e\uff0c\u767b\u5f55\u5931\u8d25\uff0c\u5c06 Promise \u8bbe\u7f6e\u4e3a Rejected \u72b6\u6001\n    reject({\n      code: 1001,\n      message: '\u5bc6\u7801\u4e0d\u6b63\u786e\uff0c\u767b\u5f55\u5931\u8d25',\n    });\n  });\n};\n\n\n\/\/ \u6a21\u62df\u767b\u5f55\u6559\u52a1\u7cfb\u7edf\nconst loginEducationSystem = (name, pwd) => {\n  \/\/ \u767b\u5f55\u903b\u8f91...\n  \/\/ \u767b\u5f55\u6210\u529f\n  resolve();\n  \/\/ \u767b\u5f55\u5931\u8d25\n  reject({\n    code: 1002,\n    message: '\u6a21\u62df\u767b\u5f55\u6559\u52a1\u7cfb\u7edf\u5931\u8d25',\n  });\n};\n\n\n\/\/ \u5c06\u7528\u6237\u540d\u5bc6\u7801\u5b58\u5165\u6570\u636e\u5e93\nconst saveUserToDB(name, pwd) => {\n  \/\/ \u6570\u636e\u5e93\u5b58\u50a8\u64cd\u4f5c\n  if (dbError) {\n    \/\/ \u6570\u636e\u5e93\u5b58\u50a8\u51fa\u9519\uff0c\u5c06 promise \u8bbe\u7f6e\u4e3a rejected\n    reject({\n      code: 1004,\n      message: '\u6570\u636e\u5e93\u5b58\u50a8\u51fa\u9519\uff0c\u5c06\u51fa\u73b0\u5f02\u5e38',\n    });\n  }\n  \/\/ \u6570\u636e\u5e93\u5b58\u50a8\u64cd\u4f5c\u6210\u529f\n  resolve();\n};\n\n\nfindUserByName(name)\n.then(() => {\n  return loginEducationSystem(name, pwd);\n})\n.then(() => {\n  return saveUserToDB(name, pwd);\n})\n.catch(e => {\n  \/\/ \u5224\u65ad\u5f02\u5e38\u51fa\u73b0\u539f\u56e0\n  if (e.notRealPromiseException) {\n    \/\/ \u6b63\u5e38\u4e2d\u6b62 promise \u800c\u6545\u610f\u8bbe\u7f6e\u7684\u5f02\u5e38\n    return res.json(e.data);\n  }\n  \/\/ \u51fa\u73b0\u9519\u8bef\u6216\u5f02\u5e38\n  return res.json(e);\n});\n<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5b9e\u4f8b\u5316\u4e86\u4e09\u4e2a promise \u5bf9\u8c61\uff0c\u5206\u522b\u5b9e\u73b0\u4e1a\u52a1\u9700\u6c42\u4e2d\u7684\u4e09\u4e2a\u529f\u80fd\u3002\u7136\u540e\u901a\u8fc7 promise \u65b9\u6cd5\u94fe\u6765\u8c03\u7528\u3002\u76f8\u6bd4\u7528\u56de\u8c03\u51fd\u6570\u800c\u8a00\uff0c\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\uff0c\u4e5f\u66f4\u6613\u8bfb\u6613\u61c2\u8026\u5408\u5ea6\u66f4\u4f4e\u66f4\u6613\u6269\u5c55\u4e86\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Promise.all &amp;&amp; Promise.race<\/h5>\n\n\n\n<p>\u4ed4\u7ec6\u89c2\u5bdf\u53ef\u4ee5\u53d1\u73b0\uff0c\u5728\u4e0a\u9762\u7684 promise \u4ee3\u7801\u4e2d\uff0cloginEducationSystem \u548c saveUserToDB \u4e24\u4e2a\u65b9\u6cd5\u6267\u884c\u6709\u5148\u540e\u987a\u5e8f\u8981\u6c42\uff0c\u4f46\u6ca1\u6709\u6570\u636e\u4f20\u9012\u3002<\/p>\n\n\n\n<p>\u5176\u5b9e promise \u65b9\u6cd5\u94fe\u66f4\u597d\u7528\u7684\u4e00\u70b9\u662f\uff0c\u5f53\u4e0b\u4e00\u4e2a\u64cd\u4f5c\u4f9d\u8d56\u4e8e\u4e0a\u4e00\u4e2a\u64cd\u4f5c\u7684\u7ed3\u679c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u901a\u8fc7 then \u65b9\u6cd5\u7684\u53c2\u6570\u6765\u4f20\u9012\u6570\u636e\u3002\u524d\u9762\u9875\u63d0\u5230\u8fc7\uff0c\u4e0b\u4e00\u4e2a then \u65b9\u6cd5\u7684\u53c2\u6570\u5c31\u662f\u4e0a\u4e00\u4e2a then \u65b9\u6cd5\u91cc\u9762 resolve \u7684\u53c2\u6570\uff0c\u6240\u4ee5\u5f53\u7136\u5c31\u53ef\u4ee5\u628a\u4e0a\u4e00\u4e2a then \u65b9\u6cd5\u7684\u6267\u884c\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a then \u65b9\u6cd5\u4e86\u3002<\/p>\n\n\n\n<p>\u8fd8\u6709\u4e9b\u65f6\u5019\uff0c\u53ef\u80fd then \u65b9\u6cd5\u7684\u6267\u884c\u987a\u5e8f\u4e5f\u6ca1\u6709\u592a\u591a\u8981\u6c42\uff0c\u53ea\u9700\u8981 promise \u65b9\u6cd5\u94fe\u4e2d\u7684\u4e24\u4e2a\u6216\u591a\u4e2a promise \u5168\u90e8\u90fd\u6267\u884c\u6b63\u786e\u3002\u8fd9\u65f6\uff0c\u5982\u679c\u4f9d\u65e7\u4e00\u4e2a\u4e00\u4e2a\u53bb\u5199 then \u53ef\u80fd\u5c31\u6bd4\u8f83\u9ebb\u70e6\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function p1() {\n  return new Promise((resolve) => {\n    console.log(1);\n    resolve();\n  });\n}\n\nfunction p2() {\n  return new Promise((resolve) => {\n    console.log(2);\n    resolve();\n  });\n}\n\nfunction p3() {\n  return new Promise((resolve) => {\n    console.log(3);\n    resolve();\n  });\n}\n<\/pre>\n\n\n\n<p>\u73b0\u5728\u53ea\u9700\u8981 p1 p2 p3 \u8fd9\u4e09\u4e2a promise \u90fd\u6267\u884c\uff0c\u5e76\u4e14 promise \u6700\u7ec8\u72b6\u6001\u90fd\u662f Fulfilled\uff0c\u90a3\u4e48\u5982\u679c\u8fd8\u662f\u4f7f\u7528\u65b9\u6cd5\u94fe\uff0c\u8fd9\u662f\u8fd9\u6837\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">p1()\n.then(() => {\n  return p2();\n})\n.then(() => {\n  return p3();\n})\n.then(() => {\n  console.log('all done');\n})\n.catch(e => {\n  console.log('e: ', e);\n});\n\n\/\/ \u8f93\u51fa\u7ed3\u679c\uff1a\n\/\/ 1\n\/\/ 2\n\/\/ 3\n\/\/ all done\n<\/pre>\n\n\n\n<p>\u4ee3\u7801\u8c8c\u4f3c\u5c31\u4e0d\u90a3\u4e48\u7cbe\u70bc\u4e86\u3002\u8fd9\u4e2a\u65f6\u5019\u5c31\u6709\u4e86 Promise.all \u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>Promise.all \u63a5\u6536\u4e00\u4e2a promise\u5bf9\u8c61\u7684\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0c\u5f53\u8fd9\u4e2a\u6570\u7ec4\u91cc\u7684\u6240\u6709 promise \u5bf9\u8c61\u5168\u90e8\u53d8\u4e3a resolve \u6216 reject \u72b6\u6001\u7684\u65f6\u5019\uff0c\u5b83\u624d\u4f1a\u53bb\u8c03\u7528 then \u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f\uff0c\u8c03\u7528\u8fd9\u51e0\u4e2a promise \u7684\u4ee3\u7801\u5c31\u53ef\u4ee5\u8fd9\u6837\u5199\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">p1()\n.then(() => {\n  return Promise.all([\n    p2(),\n    p3(),\n  ]);\n})\n.then(() => {\n  console.log('all done');\n})\n.catch((e) => {\n  console.log('e: ', e);\n});\n\n\/\/ \u8f93\u51fa\u7ed3\u679c\uff1a\n\/\/ 1\n\/\/ 2\n\/\/ 3\n\/\/ all done\n<\/pre>\n\n\n\n<p>\u8fd9\u6837\u770b\u8d77\u6765\u8c8c\u4f3c\u5c31\u7cbe\u70bc\u4e9b\u4e86\u3002<\/p>\n\n\n\n<p>\u800c\u5bf9\u4e8e Promise.race\uff0c\u5176\u53c2\u6570\u4e5f\u8ddf Promise.all \u4e00\u6837\u662f\u4e00\u4e2a\u6570\u7ec4\u3002\u53ea\u662f\u6570\u7ec4\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a promise \u5bf9\u8c61\u5982\u679c\u53d8\u4e3a resolve \u6216\u8005reject \u7684\u8bdd\uff0c\u8be5\u51fd\u6570\u5c31\u4f1a\u8fd4\u56de\uff0c\u5e76\u4f7f\u7528\u8fd9\u4e2a promise \u5bf9\u8c61\u7684\u503c\u8fdb\u884c resolve \u6216\u8005 reject\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5c31\u4e0d\u4e3e\u4f8b\u4e86\u3002<\/p>\n\n\n\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5c31\u57fa\u672c\u4e86\u89e3\u4e86 Promise \u7684\u7528\u6cd5\u53ca\u7279\u70b9\uff0c\u5e76\u5b9e\u73b0\u7528 Promise \u91cd\u6784\u7528\u56de\u8c03\u51fd\u6570\u5199\u7684\u5f02\u6b65\u64cd\u4f5c\u3002\u73b0\u5728\u5bf9 Promise \u7684\u4f7f\u7528\uff0c\u5e94\u8be5\u9a7e\u8f7b\u5c31\u719f\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u8bb2\u7684\u662f\u5982\u4f55\u5b9e\u73b0 Promise \u7684\u94fe\u5f0f\u8c03\u7528\u3002\u4e5f\u5c31\u662f promise().then().then().c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3856"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3856"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3856\/revisions"}],"predecessor-version":[{"id":3862,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3856\/revisions\/3862"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}