{"id":6300,"date":"2023-01-07T12:30:46","date_gmt":"2023-01-07T04:30:46","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6300"},"modified":"2023-01-07T12:30:46","modified_gmt":"2023-01-07T04:30:46","slug":"%e4%ba%86%e8%a7%a3node-js%e4%b8%adutil%e6%a8%a1%e7%b5%84%e7%9a%84%e5%b7%a5%e5%85%b7%e5%87%bd%e6%95%b8%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/node-js\/%e4%ba%86%e8%a7%a3node-js%e4%b8%adutil%e6%a8%a1%e7%b5%84%e7%9a%84%e5%b7%a5%e5%85%b7%e5%87%bd%e6%95%b8%e6%93%8d%e4%bd%9c\/","title":{"rendered":"\u4e86\u89e3Node.js\u4e2dutil\u6a21\u7d44\u7684\u5de5\u5177\u51fd\u6578\u64cd\u4f5c"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Node.js, util, \u5de5\u5177\u51fd\u6578, \u64cd\u4f5c\"><\/p>\n<p>Node.js \u662f\u4e00\u500b\u5ee3\u53d7\u6b61\u8fce\u7684 JavaScript \u74b0\u5883\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u4f7f\u7528 JavaScript \u4f86\u69cb\u5efa\u53ef\u57f7\u884c\u7684\u61c9\u7528\u7a0b\u5e8f\u3002Node.js \u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u6a21\u584a\u7cfb\u7d71\uff0c\u5176\u4e2d\u5305\u62ec\u4e00\u500b\u540d\u70ba &#8216;util&#8217; \u7684\u6a21\u584a\uff0c\u53ef\u4ee5\u7528\u65bc\u64cd\u4f5c\u5e38\u898b\u7684\u5de5\u5177\u51fd\u6578\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 &#8216;util&#8217; \u6a21\u584a\u4f86\u64cd\u4f5c\u5e38\u898b\u7684\u5de5\u5177\u51fd\u6578\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f &#8216;util&#8217; \u6a21\u584a\uff1f<\/h2>\n<p>&#8216;util&#8217; \u6a21\u584a\u662f Node.js \u4e2d\u7684\u4e00\u500b\u5167\u7f6e\u6a21\u584a\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u5de5\u5177\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u65bc\u64cd\u4f5c\u5b57\u7b26\u4e32\u3001\u6578\u7d44\u3001\u5c0d\u8c61\u7b49\u6578\u64da\u985e\u578b\u3002\u5b83\u9084\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u7684\u529f\u80fd\uff0c\u5982\u6df1\u5ea6\u6bd4\u8f03\u3001\u6df1\u5ea6\u514b\u9686\u3001\u6df1\u5ea6\u62f7\u8c9d\u7b49\u3002<\/p>\n<h2>\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 &#8216;util&#8217; \u6a21\u584a\uff1f<\/h2>\n<p>\u8981\u5728 Node.js \u4e2d\u4f7f\u7528 &#8216;util&#8217; \u6a21\u584a\uff0c\u9996\u5148\u9700\u8981\u5c0e\u5165\u5b83\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst util = require('util');\n<\/pre>\n<p>\u7136\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 &#8216;util&#8217; \u6a21\u584a\u63d0\u4f9b\u7684\u51fd\u6578\u4f86\u64cd\u4f5c\u5e38\u898b\u7684\u5de5\u5177\u51fd\u6578\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 &#8216;util.inspect()&#8217; \u51fd\u6578\u4f86\u5c0d\u5c0d\u8c61\u9032\u884c\u6df1\u5ea6\u6aa2\u67e5\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst obj = {\n  name: 'John',\n  age: 30\n};\n\nconsole.log(util.inspect(obj));\n\/\/ { name: 'John', age: 30 }\n<\/pre>\n<p>\u6b64\u5916\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528 &#8216;util.promisify()&#8217; \u51fd\u6578\u5c07\u56de\u8abf\u51fd\u6578\u8f49\u63db\u70ba Promise \u5c0d\u8c61\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst fs = require('fs');\n\nconst readFile = util.promisify(fs.readFile);\n\nreadFile('file.txt')\n  .then(data => {\n    console.log(data);\n  })\n  .catch(err => {\n    console.error(err);\n  });\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Node.js \u4e2d\u7684 &#8216;util&#8217; \u6a21\u584a\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u5de5\u5177\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u65bc\u64cd\u4f5c\u5b57\u7b26\u4e32\u3001\u6578\u7d44\u3001\u5c0d\u8c61\u7b49\u6578\u64da\u985e\u578b\u3002\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Node.js \u4e2d\u4f7f\u7528 &#8216;util&#8217; \u6a21\u584a\u4f86\u64cd\u4f5c\u5e38\u898b\u7684\u5de5\u5177\u51fd\u6578\uff0c\u5305\u62ec &#8216;util.inspect()&#8217; \u548c &#8216;util.promisify()&#8217; \u7b49\u51fd\u6578\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3Node.js\u4e2dutil\u6a21\u7d44\u7684\u5de5\u5177\u51fd\u6578\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u6709\u6548\u7387\u7684\u958b\u767c\u51fa\u66f4\u597d\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39util\u6a21\u7d44\u7684\u529f\u80fd\u53ca\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[193,192],"tags":[191,190],"class_list":["post-6300","post","type-post","status-publish","format-standard","hentry","category-node","category-node-js","tag-node","tag-node-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1DC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6300"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6300\/revisions"}],"predecessor-version":[{"id":6301,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6300\/revisions\/6301"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}