{"id":244,"date":"2022-04-07T04:54:28","date_gmt":"2022-04-07T04:54:28","guid":{"rendered":"http:\/\/114.132.224.28\/?p=244"},"modified":"2022-04-07T04:54:28","modified_gmt":"2022-04-07T04:54:28","slug":"%e4%b8%80%e6%ad%a5%e6%ad%a5%e5%ae%9e%e7%8e%b0aysnc-await","status":"publish","type":"post","link":"http:\/\/localhost\/2022\/04\/07\/%e4%b8%80%e6%ad%a5%e6%ad%a5%e5%ae%9e%e7%8e%b0aysnc-await\/","title":{"rendered":"\u4e00\u6b65\u6b65\u5b9e\u73b0aysnc await"},"content":{"rendered":"\n
async\/await\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898\uff1a<\/p>\n\n\n\n
async\/await\u53ef\u4ee5\u89e3\u51b3promise.then\u8bed\u6cd5\u94fe\u5f0f\u8c03\u7528\u7684\u7e41\u7410\u95ee\u9898\uff0c\u901a\u8fc7.next()\u4f20\u53c2\u5f62\u5f0f\uff0c\u628a\u8868\u8fbe\u5f0f\u7684\u503c\u91cd\u65b0\u6ce8\u5165\u5230\u51fd\u6570\u91cc\u9762\uff0c\u53ef\u4ee5\u50cf\u540c\u6b65\u8c03\u7528\u90a3\u6837\u62ff\u5230\u5f02\u6b65\u51fd\u6570\u7684\u7ed3\u679c\u503c\uff0c\u5728\u8c03\u7528generator\u51fd\u6570\u65f6\uff0c\u91cc\u9762\u7684\u51fd\u6570\u4e5f\u4e0d\u4f1a\u5168\u90e8\u6267\u884c\uff0c\u800c\u662f\u9700\u8981.next()\u4e00\u6b65\u4e00\u6b65\u8fd0\u884c\u5230yield\u7684\u8282\u70b9\uff0c\u8fd9\u6837\u5728yield\u8868\u8fbe\u5f0f\u4e0b\u7684\u51fd\u6570\uff0c\u5c31\u9700\u8981\u7b49\u5230\u5916\u90e8\u51fd\u6570\u8c03\u7528.next()\u4e4b\u540e\u8c03\u7528\uff0c\u8fd9\u6837\u7533\u660e\u548c\u8c03\u7528\u51fd\u6570\u7684\u903b\u8f91\u5c31\u53ef\u4ee5\u5199\u5728\u4e00\u8d77\u3002\u8fd9\u4e2a\u662f\u901a\u8fc7Iterator\u65e0\u6cd5\u5b9e\u73b0\u7684\uff08\u4e0b\u6587\u4f1a\u8be6\u7ec6\u89e3\u91ca\uff09<\/p>\n\n\n\n
P.S.: \u4efb\u4f55\u6709\u7591\u95ee\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u901a\u8fc7debugger\u6765\u6253\u65ad\u70b9\u53bb\u7406\u89e3\u3002\u901a\u8fc7npm run debug<\/p>\n\n\n\n
\u524d\u7f6e\u77e5\u8bc6\uff1a<\/p>\n\n\n\n
async\/await\u662fgenerator\/yield\u7684\u8bed\u6cd5\u7cd6<\/p>\n\n\n\n
generator\/yield[\u6269\u5c55\u9605\u8bfb\uff1agenerator\/yield\u662f\u4ec0\u4e48?<\/a>]<\/p>\n\n\n\n next\/throw\/return\u80fd\u8ba9\u6211\u4eec\u4ece\u5916\u9762\u5411generator\u4f20\u503c<\/p>\n\n\n\n next \u6267\u884cyield\u7684\u8868\u8fbe\u5f0f\uff0c\u5e76\u5f97\u5230{done ,value}\u5bf9\u8c61\uff0c\u4f7f\u5916\u5c42\u51fd\u6570\u77e5\u9053generator\u662f\u5426\u5df2\u7ecf\u6267\u884c\u5b8c\uff0cyield\u7684\u8868\u8fbe\u5f0f\u6c42\u503c\u540e\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\uff0cnext\u4f20\u9012\u7684\u53c2\u6570\uff0c\u8d4b\u503c\u7ed9result<\/p>\n\n\n\n throw\u53ef\u4ee5\u8ba9generator\u629b\u51fa\u5f02\u5e38\uff0c\u7ec8\u6b62generator\uff0c\u4f20\u9012\u9519\u8bef\u53c2\u6570\uff0c\u4f1a\u88ab\u5185\u90e8\u7684catch\u6355\u83b7\u4f7f\u7528<\/p>\n\n\n\n return\u53ef\u4ee5\u8ba9generator\u7ec8\u6b62\uff0cvalue\u503c\u4e3areturn\u4f20\u7684\u53c2\u6570<\/p>\n\n\n\n generator\/yield\u63d0\u4f9b\u4e86\u4e00\u79cd\u5916\u90e8\u51fd\u6570\u548c\u5185\u90e8\u51fd\u6570\u4f20\u9012\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u5b9e\u73b0\u903b\u8f91\u5206\u79bb\uff0c\u90a3\u4e48\u4e00\u6b65\u6b65\u8c03\u7528next\u51fd\u6570\u6bd4\u8f83\u9ebb\u70e6\u7684\uff0c\u6709\u6ca1\u6709\u4e00\u79cd\u81ea\u52a8\u6267\u884c\u7684\u65b9\u6cd5\uff0c\u81ea\u52a8\u6267\u884c\u5c31\u662f\u901a\u8fc7\u9012\u5f52\u5e76\u4f20\u9012\u53c2\u6570\u7684\u5f62\u5f0f\u5b9e\u73b0\u7684<\/p>\n\n\n\n \u90a3\u4e48\u6211\u4eec\u5148\u5b9e\u73b0\u4e00\u4e2a\u540c\u6b65\u7248\u672c\u7684\u81ea\u52a8\u6267\u884c\u5668<\/p>\n\n\n\n \u4ee3\u7801\u5730\u5740<\/a><\/p>\n\n\n\n \u8fd9\u91cc\u662f\u540c\u6b65\u8c03\u7528\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u62c5\u5fc3\u629b\u9519\u80fd\u4e0d\u80fd\u88ab\u5192\u6ce1\u6355\u83b7\uff0cyield\u8868\u8fbe\u5f0f\u5982\u679c\u629b\u9519\uff0cgenerator\u51fd\u6570\u5c31\u4f1a\u7ec8\u6b62\u8fed\u4ee3\uff0c\u7406\u89e3\u4e86\u540c\u6b65\u8c03\u7528\uff0c\u90a3\u4e48\u5f02\u6b65\u8c03\u7528\u53ea\u9700\u8981\u89e3\u51b3\u597dpromise.then\u7684\u95ee\u9898\uff0c\u628aresolve\u7684value\u8fd4\u56de\u5230generator\u51fd\u6570\uff0c\u6216\u8005reject\u7684error\u5e26\u5230\u51fd\u6570\u7ec8\u6b62\u8fed\u4ee3\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n \u4ee3\u7801\u5730\u5740<\/a><\/p>\n\n\n\n \u90a3\u6211\u4eec\u518d\u770b\u770b\u7ec6\u770b\u5904\u7406\u9519\u8bef\u7684\u95ee\u9898\uff0c\u6211\u4eec\u6709\u65f6\u4f1a\u9047\u5230UnhandledPromiseRejectionWarning<\/em>\uff0c\u8fd9\u662f\u54ea\u91cc\u51fa\u95ee\u9898\u4e86\u5462\uff1f<\/p>\n\n\n\n \u6211\u4eec\u4e0d\u518d.catch\u6355\u83b7async\u8fd4\u56de\u7684promise\u5f02\u5e38\u6539\u6210try\/catch\u5f62\u5f0f<\/p>\n\n\n\n \u4ee3\u7801\u5730\u5740<\/a><\/p>\n\n\n\n \u8fd9\u91cc\u7531\u4e8easync\u5185\u90e8\u6709\u9519\u8bef\uff0c\u800c\u5185\u90e8\u4e5f\u6ca1\u6709try\/catch\u53bb\u6355\u83b7\uff0c\u90a3\u4e48\u8fd9\u4e2a\u9519\u8bef\u5c31\u4f1a\u88abautoRun\u6267\u884c\u7684\u65f6\u5019\u6355\u83b7\u5230\uff0c\u7136\u540e\u5c06\u8fd4\u56de\u7684promise\u7684\u72b6\u6001\u8bbe\u4e3areject\uff0c\u800c\u5916\u90e8\u8c03\u7528\u7684\u65f6\u5019\u6ca1\u6709\u5904\u7406\u8fd9\u4e2areject\u5c31\u4f1a\u5bfc\u81f4UnhandledPromiseRejectionWarning<\/em>\uff0c\u77e5\u9053\u4ec0\u4e48\u539f\u56e0\u4e4b\u540e\uff0c\u8981\u89e3\u51b3\u5c31\u5f88\u7b80\u5355\u8981\u4e0d\u518d\u5305async,await,\u8981\u4e0d\u5c31.catch\u53bb\u6355\u83b7\u3002\u5982\u679c\u5728\u5185\u90e8\u6709try\/catch\u53bb\u6355\u83b7\uff0c\u5c31\u4e0d\u4f1a\u8d70\u5230autoRun\u7684catch\uff0c\u800c\u662f\u7ee7\u7eed\u6267\u884c\u5230\u4e0b\u4e00\u4e2ayield\u3002<\/p>\n\n\n\n \u90a3\u4e48\u8fd9\u91cc\u603b\u7ed3\u4e00\u4e0b\u9519\u8bef\u5904\u7406\uff1a<\/p>\n\n\n\n \u4ee3\u7801\u5730\u5740<\/a><\/p>\n\n\n\n \u4e0b\u9762\u6765\u63a2\u8ba8\u4e00\u4e0b\u5982\u679c\u4e0d\u7528generator\uff0c\u5355\u7eaf\u901a\u8fc7Iterator\u65b9\u5f0f\u8981\u600e\u4e48\u5b9e\u73b0<\/p>\n\n\n\n \u6211\u4eec\u77e5\u9053generator\u8fd4\u56de\u7684\u5c31\u662f\u4e00\u4e2a\u8fed\u4ee3\u5bf9\u8c61\uff0c\u90a3\u6211\u4eec\u5982\u679c\u4e0d\u7528generator\uff0c\u80fd\u4e0d\u80fd\u4e5f\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c<\/p>\n\n\n\n \u6211\u4eec\u6765\u5148\u770b\u4e00\u822c\u6211\u4eec\u662f\u600e\u4e48\u5b9e\u73b0promise\u7684\u987a\u5e8f\u6267\u884c\u7684<\/p>\n\n\n\n \u4ee3\u7801\u5730\u5740<\/a><\/p>\n\n\n\n \u5982\u679c\u6211\u4eec\u60f3\u514d\u53bbpromise.then\u8bed\u6cd5\u8ba9\u4ed6\u987a\u5e8f\u6267\u884c\uff0c\u90a3\u4e48\u8fd9\u4e9b\u5907\u8c03\u7528\u7684\u51fd\u6570\u9700\u8981\u5b58\u5728generator\u51fd\u6570\u91cc\u9762\u3002\u6211\u4eec\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u4ed6\u4eec<\/p>\n\n\n\n\/\/ \u58f0\u660e\u4e00\u4e2aGenerator\u51fd\u6570\nfunction* gen () { \n const result = yield \u8868\u8fbe\u5f0f\n}\n\/\/ \u8c03\u7528\nconst g = gen()\ng.next(param)\ng.throw(param)\ng.return(param)<\/code><\/pre>\n\n\n\n
\/\/ \u5b9e\u73b0async,await\n\/\/ \u5148\u8003\u8651generation yield\u7684\u540c\u6b65\u7248\u672c\nfunction* g() {\n const a = yield syncFn('hi')\n console.log('a', a)\n const b = yield syncErrorFn(a + 'sync')\n console.log('a', b)\n return b\n}\n\nfunction syncFn(a) {\n return a \n}\n\nfunction syncErrorFn(a) {\n throw(new Error(a))\n}\n\nfunction syncStep(gen) {\n const g = gen()\n function autoRun(property, preVal) {\n let next;\n next = g[property](preVal)\n \n if(next.done) {\n return next.value\n }else{\n return autoRun('next', next.value)\n }\n }\n try {\n return autoRun('next', undefined)\n } catch (error) {\n console.log('outer', error.message)\n }\n \n}\n\nsyncStep(g)<\/code><\/pre>\n\n\n\n
\/\/ \u5b9e\u73b0\u5f02\u6b65\u7248\u672c\nfunction* asyncG() {\n const a = yield asyncFn('hi')\n console.log('a', a)\n const b = yield asyncErrorFn(a + 'async')\n debugger\n console.log('b', b)\n return b \n}\n\nfunction asyncFn(a) {\n return new Promise((resolve, reject) => {\n setTimeout(() => {\n resolve(a)\n },1000)\n })\n}\n\nfunction asyncErrorFn(a) {\n return new Promise((resolve, reject) => {\n setTimeout(() => {\n reject(a)\n },500)\n })\n}\n\nfunction step(gen) {\n const g = gen()\n return new Promise((resolve, reject) => {\n function autoRun(property, preVal) {\n let next;\n try {\n next = g[property](preVal)\n debugger\n } catch (error) {\n return reject(error) \/\/ \u8fd9\u91ccreject\u7684\u540c\u65f6\u9700\u8981\u7ec8\u6b62\u9012\u5f52\n }\n if(next.done) {\n return resolve(next.value)\n }\n next.value.then((resolve) => {\n autoRun(\"next\", resolve)\n }).catch((error) => {\n autoRun(\"throw\", error)\n })\n }\n autoRun('next', undefined)\n })\n}\n\nstep(asyncG).then((resolve) => {\n console.log('step', resolve)\n}).catch((error) => {\n console.log('stepError', error)\n})<\/code><\/pre>\n\n\n\n
async function canNotCatchFn() {\n try {\n \/\/ UnhandledPromiseRejectionWarning\n step(asyncG) \/\/ \u8fd9\u91cc\u4f1a\u6355\u83b7\u4e0d\u4e86\u9519\u8bef\uff0c\u8981\u4e0d\u5c31\u662f\u518d\u5305async,await,\u8981\u4e0d\u5c31.catch\u53bb\u6355\u83b7\n \/\/ await step(asyncG)\n } catch (error) {\n console.log(error)\n }\n}\n\ncanNotCatchFn()<\/code><\/pre>\n\n\n\n
\/\/ \u6a21\u62dfservice\u60c5\u51b5\nconst getEntityCatch = async() => {\n try {\n const data = await getFailEntity()\n return data\n } catch (error) {\n console.log('getEntityError', error)\n }\n}\nconst getFailEntity = async() => {\n const data = await mockFetchData(false)\n return data\n}\nconst getSuccessEntity = async() => {\n const data = await mockFetchData(true)\n return data\n}\n\nconst mockFetchData = (success) => {\n return new Promise((resolve, reject) => {\n if(success) {\n resolve([1,2,3])\n }else{\n reject(new Error('fail'))\n }\n })\n}\n\n\/\/ \u4e0d\u76f8\u5173\u7684\u6b65\u9aa4\u60f3\u987a\u5e8f\u6267\u884c\nconst uiNotRelativeFn = async() => {\n const data1 = await getEntityCatch()\n console.log('uiNotRelativeFn', data1)\n const data2 = await getSuccessEntity() \/\/ \u6ce8\u610f\u8fd9\u91cc\u629b\u51fa\u7684\u9519\u8bef\u6ca1\u6709\u88ab\u6355\u83b7\n console.log('uiNotRelativeFn', data2)\n}\n\nuiNotRelativeFn() \/\/ \u5982\u679cdata2\u629b\u51fa\u9519\u8bef\uff0c\u5219\u4f1a\u6355\u83b7\u4e0d\u5230data2\u7684\u9519\u8bef\n\n\/\/ \u76f8\u5173\u7684\u6b65\u9aa4\u60f3\u987a\u5e8f\u6267\u884c\nconst uiRelativeFn = async() => {\n try {\n const data1 = await getFailEntity()\n console.log('uiRelativeFn', data1)\n const data2 = await getSuccessEntity()\n console.log('uiRelativeFn', data2)\n } catch (error) {\n console.log('uiRelativeFnError', error)\n }\n}\n\nuiRelativeFn()\n\n\/\/ \u76f8\u5173\u7684\u6b65\u9aa4\u60f3\u987a\u5e8f\u6267\u884c\nconst uiRelativeNotCatchFn = async() => {\n const data1 = await getFailEntity()\n console.log('uiRelativeNotCatchFn', data1)\n const data2 = await getSuccessEntity()\n console.log('uiRelativeNotCatchFn', data2)\n}\n\nconst uiRelativeNotCatchNormalErrorFn = async() => {\n const data2 = await getSuccessEntity()\n console.log('uiRelativeNotCatchFn', data2)\n throw(new Error(\"sync Error\"))\n const data1 = await getFailEntity()\n console.log('uiRelativeNotCatchFn', data1)\n}\n\nuiRelativeNotCatchNormalErrorFn().catch(error => {\n console.log('uiRelativeNotCatchNormalErrorFn', error)\n})<\/code><\/pre>\n\n\n\n
\/\/ promise\u7248\u672c\nfunction promiseFn() {\n return asyncFn(\"hi\")\n .then((resolve) => asyncFn(resolve + \"async\"))\n .then((resolve) => asyncErrorFn(resolve))\n}\n\npromiseFn().then((resolve) => {\n console.log('promiseFn', resolve)\n}).catch((error) => {\n console.log('promiseFnError', error)\n})\n<\/code><\/pre>\n\n\n\n