{"id":260468,"date":"2023-01-28T09:09:52","date_gmt":"2023-01-28T01:09:52","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=260468"},"modified":"2022-12-27T23:11:28","modified_gmt":"2022-12-27T15:11:28","slug":"await-linux-await","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/await-linux-await.html","title":{"rendered":"await \u9519\u8bef\u6355\u83b7\u5b9e\u73b0\u65b9\u5f0f\u6e90\u7801\u793a\u4f8b\u89e3\u6790"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86await \u9519\u8bef\u6355\u83b7\u5b9e\u73b0\u65b9\u5f0f\u6e90\u7801\u793a\u4f8b\u89e3\u6790\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u524d\u8a00<\/strong><\/div>\n

Promise \u662f\u4e00\u79cd\u5728 JavaScript \u4e2d\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u673a\u5236\u3002Promise \u5728\u5f00\u53d1\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\uff0c\u8fd9\u7bc7\u6587\u7ae0\u5c06\u5b66\u4e60\u5982\u4f55\u4f18\u96c5\u7684\u6355\u83b7 await \u7684\u9519\u8bef\u3002<\/p>\n

\u8d44\u6e90\uff1a<\/p>\n

\u4ed3\u5e93\u5730\u5740\uff1aawait-to-js<\/p>\n

\u53c2\u8003\u6587\u7ae0\uff1aHow to write async await without try-catch blocks in Javascript (grossman.io)<\/p>\n

Promise \u7684\u4f7f\u7528\u65b9\u6cd5<\/strong><\/div>\n
  • \u521b\u5efa\u4e00\u4e2a Promise \u5b9e\u4f8b\u3002\u8fd9\u901a\u5e38\u662f\u901a\u8fc7\u8c03\u7528 Promise \u6784\u9020\u51fd\u6570\u6765\u5b8c\u6210\u7684\uff0c\u5e76\u5c06\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6784\u9020\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1aresolve \u548c reject\u3002resolve \u548c reject \u5206\u522b\u7528\u4e8e\u5904\u7406 Promise \u7684\u6210\u529f\u548c\u5931\u8d25\u7ed3\u679c\u3002<\/li>\n
  • \u5728\u51fd\u6570\u4f53\u5185\u6267\u884c\u5f02\u6b65\u64cd\u4f5c\u3002\u5f53\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u65f6\uff0c\u5982\u679c\u6267\u884c\u6210\u529f\uff0c\u5219\u8c03\u7528 resolve \u51fd\u6570\u6765\u5c06 Promise \u7684\u72b6\u6001\u53d8\u4e3a\u201c\u5df2\u5b8c\u6210\u201d\uff0c\u5e76\u5c06\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 resolve \u51fd\u6570\uff1b\u5982\u679c\u6267\u884c\u5931\u8d25\uff0c\u5219\u8c03\u7528 reject \u51fd\u6570\u5c06 Promise \u7684\u72b6\u6001\u53d8\u4e3a\u201c\u5df2\u62d2\u7edd\u201d\uff0c\u5e76\u5c06\u5931\u8d25\u7684\u539f\u56e0\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 reject \u51fd\u6570\u3002<\/li>\n
  • \u4f7f\u7528 then \u65b9\u6cd5\u6765\u5904\u7406 Promise \u7684\u6210\u529f\u548c\u5931\u8d25\u7ed3\u679c\u3002then \u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u5904\u7406\u6210\u529f\u7ed3\u679c\u7684\u51fd\u6570\u548c\u4e00\u4e2a\u5904\u7406\u5931\u8d25\u7ed3\u679c\u7684\u51fd\u6570\u3002\u5728 Promise \u72b6\u6001\u53d8\u4e3a\u201c\u5df2\u5b8c\u6210\u201d\u6216\u201c\u5df2\u62d2\u7edd\u201d\u540e\uff0cthen \u65b9\u6cd5\u4f1a\u81ea\u52a8\u6267\u884c\u76f8\u5e94\u7684\u5904\u7406\u51fd\u6570\u3002<\/li>\n

    \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

    const myPromise = new Promise(function(resolve, reject) {\r\n  \/\/ \u6267\u884c\u5f02\u6b65\u64cd\u4f5c\r\n  setTimeout(function() {\r\n    \/\/ \u5f02\u6b65\u64cd\u4f5c\u6210\u529f\r\n    resolve('Success');\r\n  }, 1000);\r\n});\r\n\/\/ \u5904\u7406 Promise \u7684\u6210\u529f\u548c\u5931\u8d25\u7ed3\u679c\r\nmyPromise.then(function(result) {\r\n  \/\/ \u5904\u7406\u6210\u529f\u7ed3\u679c\r\n  console.log(result);\r\n}).catch(function(err){\r\n  \/\/ \u5904\u7406\u5931\u8d25\r\n  console.log(err)\r\n})<\/pre>\n

    \u5f02\u6b65\u51fd\u6570\u6b63\u5728\u7b49\u5f85 Promise\uff0c\u56e0\u6b64\u5f53 promise \u9047\u5230\u9519\u8bef\u65f6\uff0c\u5b83\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u8be5\u5f02\u5e38\u5c06\u5728 promise \u7684 catch \u65b9\u6cd5\u4e2d\u6355\u83b7\u3002<\/p>\n

    \u5176\u4ed6\u5904\u7406\u9519\u8bef\u7684\u65b9\u5f0f\uff1a<\/p>\n

    try{\r\n    const res = await myPromise()\r\n}catch(e){\r\n    console.log(e)\r\n}<\/pre>\n

    \u5f53\u4ee3\u7801\u4e2d\u6709\u5927\u91cf\u7684\u5f02\u6b65\u64cd\u4f5c\u65f6\uff0c\u5c31\u9700\u8981\u6709\u5f88\u591a\u7684try\/catch \u5757\uff0c\u4ee3\u7801\u770b\u8d77\u6765\u5c31\u5f88\u81c3\u80bf\uff0c\u90a3\u4e48\u6709\u4ec0\u4e48\u66f4\u597d\u7684\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u7684\u529e\u6cd5\u5462\uff1f<\/p>\n

    await-to-js<\/strong><\/div>\n

    \u8fd9\u4e2a\u9879\u76ee\u7684 slogan \u662f\uff1a<\/p>\n

    Async await wrapper for easy error handling<\/p>\n

    \u5f02\u6b65\u7b49\u5f85\u5305\u88c5,\u65b9\u4fbf\u9519\u8bef\u5904\u7406<\/p>\n

    \u7528\u6cd5\uff1a<\/p>\n

    import to from 'await-to-js';\r\n[ err, user ] = await to(UserModel.findById(1));\r\nif(!user) return cb('No user found');<\/pre>\n

    to \u51fd\u6570\u63a5\u6536\u4e00\u4e2a Promise\uff0c\u7136\u540e\u5c06\u6210\u529f\u54cd\u5e94\u89e3\u6790\u4e3a\u6570\u7ec4\uff0c\u5e76\u5c06\u8fd4\u56de\u6570\u636e\u4f5c\u4e3a\u7b2c\u4e8c\u9879\u3002\u4ece\u6355\u83b7\u4e2d\u6536\u5230\u7684\u9519\u8bef\u4f5c\u4e3a\u7b2c\u4e8c\u9879\u3002<\/p>\n

    \u6e90\u7801<\/strong><\/div>\n
    \/**\r\n * @param { Promise } promise\r\n * @param { Object= } errorExt - Additional Information you can pass to the err object\r\n * @return { Promise }\r\n *\/\r\nexport function to (\r\n  promise: Promise,\r\n  errorExt?: object\r\n): Promise<[U, undefined] | [null, T]> {\r\n  return promise\r\n    .then<[null, T]>((data: T) => [null, data])\r\n    .catch<[U, undefined]>((err: U) => {\r\n      if (errorExt) {\r\n        const parsedError = Object.assign({}, err, errorExt);\r\n        return [parsedError, undefined];\r\n      }\r\n      return [err, undefined];\r\n    });\r\n}\r\nexport default to;<\/t><\/pre>\n

    \u63a5\u6536\u4e00\u4e2a Promise \u5b9e\u4f8b\u548c\u4e00\u4e2a\u53ef\u9009\u7684 errorExt \u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Promise\u3002<\/p>\n

    \u539f\u7406\u662f\u4f7f\u7528 then \u65b9\u6cd5\u5904\u7406 Promise \u7684\u6210\u529f\u7ed3\u679c\uff0c\u5e76\u4f7f\u7528 catch \u65b9\u6cd5\u5904\u7406 Promise \u7684\u5931\u8d25\u7ed3\u679c\u3002\u5982\u679c Promise \u6267\u884c\u6210\u529f\uff0c\u5219\u4f1a\u5c06\u7ed3\u679c\u5305\u88c5\u6210\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u5e76\u4f5c\u4e3a\u65b0\u7684 Promise \u7684\u6210\u529f\u7ed3\u679c\u8fd4\u56de\uff1b\u5982\u679c Promise \u6267\u884c\u5931\u8d25\uff0c\u5219\u4f1a\u5c06\u9519\u8bef\u5bf9\u8c61\u5305\u88c5\u6210\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u5e76\u4f5c\u4e3a\u65b0\u7684 Promise \u7684\u5931\u8d25\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n

    \u4f7f\u7528await-to-js,\u5f97\u4ee5\u4e00\u884c\u4ee3\u7801\u5b8c\u6210\u5bf9\u5f02\u6b65\u4ee3\u7801\u9519\u8bef\u7684\u6355\u83b7\uff0c\u9605\u8bfb\u6587\u7ae0\u5f97\u77e5\uff0cawait-to-js \u662f\u4f5c\u8005\u901a\u8fc7goLang\u542f\u53d1\u5f97\u6765\u7684\u7075\u611f\u3002\u66f4\u52a0\u8bf4\u660e\u4e86\u878d\u4f1a\u8d2f\u901a\uff0c\u5b66\u4ee5\u81f4\u7528\u7684\u91cd\u8981\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    Promise \u662f\u4e00\u79cd\u5728 JavaScript \u4e2d\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u673a\u5236\u3002Promise \u5728\u5f00\u53d1\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\uff0c […]<\/p>\n","protected":false},"author":1479,"featured_media":259065,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-260468","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\/260468","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\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=260468"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260468\/revisions"}],"predecessor-version":[{"id":260471,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260468\/revisions\/260471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/259065"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=260468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=260468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=260468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}