{"id":958,"date":"2019-12-16T09:31:19","date_gmt":"2019-12-16T01:31:19","guid":{"rendered":"https:\/\/sora.sound.moe\/?p=958"},"modified":"2019-12-16T09:31:19","modified_gmt":"2019-12-16T01:31:19","slug":"javascript-%e9%9a%8f%e6%9c%ba%e8%87%aa%e6%9d%80%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/sora.sound.moe\/?p=958","title":{"rendered":"JavaScript &#8211; \u968f\u673a\u81ea\u6740\uff1f"},"content":{"rendered":"\n<p>\u524d\u6bb5\u65f6\u95f4\u4f7f\u7528 Node \u5199\u4e86\u4e00\u4e9b\u5c0f\u5de5\u5177\u811a\u672c\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u547d\u4ee4\u6267\u884c\uff0c\u7ed3\u679c\u53d1\u73b0\uff0c\u5728\u8fd0\u884c\u8f83\u957f\u7684\u4efb\u52a1\u65f6\uff0c\u7ecf\u5e38\u4f1a\u53d1\u751f\u81ea\u6740\u800c\u627e\u4e0d\u5230\u539f\u56e0\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u7531\u4e8e\u6574\u4e2a\u811a\u672c\u8fd0\u884c\u5728\u4e00\u4e2a async IIFE \u4e0b\uff0c\u4e00\u5f00\u59cb\u7684\u62a5\u9519\u662f UnhandledPromiseRejection: null\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f\u6309\u7167\u4e00\u822c\u601d\u8def\u5728 process \u4e0a\u76d1\u542c\u4e00\u4e0b\u672a\u5904\u7406\u7684 promise rejection\u3002\u53d1\u73b0\u5f97\u5230\u7684\u9519\u8bef\u662f\u4e2a null\uff0c\u8fd9\u5c31\uff0c\u5f88\u8ff7\u3002<\/p>\n\n\n\n<p>\u56de\u6eda\u4e86\u51e0\u4e2a\u7248\u672c\uff0c\u53d1\u73b0\u95ee\u9898\u4f9d\u65e7\uff0c\u627e\u4e0d\u5230\u95ee\u9898\u5f15\u5165\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>\u800c\u4e14\u8fd9\u4e2a\u9519\u8bef\u662f\u968f\u673a\u89e6\u53d1\u7684\uff0c\u6709\u65f6\u5019\u4f1a\u5728 Step 1 \u89e6\u53d1\uff0c\u6709\u65f6\u5019\u5230\u4e86 Step 3 \u624d\u4f1a\u89e6\u53d1\u3002<\/p>\n\n\n\n<p>\u5ba1\u89c6\u4e86\u4e00\u4e0b\u4ee3\u7801\u91cc\u6240\u6709\u7684 reject\uff0c\u6700\u7ec8\u5c06\u76ee\u5149\u9501\u5b9a\u5728\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const exec = require('child_process').exec;\nconst execCommand = (command) => {\n    return new Promise((resolve, reject) => {\n        const child = exec(command);\n        child.stdout.on('data', (data) => {\n            console.log(data);\n        });\n        child.stderr.on('data', (data) => {\n            console.log(data);\n        });\n        child.on('close', (code, signal) => {\n            if (code === 0) {\n                resolve();\n            } else {\n                reject(code)\n            }\n        });\n    })\n}<\/code><\/pre>\n\n\n\n<p>\u5f53\u5b50\u8fdb\u7a0b\u7ed3\u675f\u7684\u65f6\u5019\uff0c\u89c2\u5bdf return code\uff0c\u4e0d\u4e3a 0 \u5219\u8bf4\u660e\u53d1\u751f\u4e86\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u6253\u5370\u4e86\u4e00\u4e0b\u53d1\u73b0\u8fd9\u4e2a code \u5c45\u7136\u662f null\u3002\u4e8e\u662f\u67e5\u4e86\u4e0b\u6587\u6863\uff0c\u53d1\u73b0\uff0ccode \u548c signal \u4e92\u65a5\uff0c\u53ea\u80fd\u6709\u4e00\u4e2a\u4e0d\u4e3a\u7a7a\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f\u518d\u6253\u5370\u4e00\u4e0b signal\uff0c\u53d1\u73b0\u662f SIGTERM\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f\u6211\u4eec\u7684\u95ee\u9898\u8f6c\u6362\u4e3a\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u53c9\u51fa\u53bb\u7684\u5b50\u8fdb\u7a0b\u4f1a\u88ab SIGTERM \u6740\u6389\uff0c\u8fd8\u662f\u968f\u673a\u53d1\u751f\u7684\u3002<\/p>\n\n\n\n<p><del>\u4e8e\u662f\u6211\u5347\u7ea7\u4e86\u4e00\u4e0b Node \u7136\u540e\u53d1\u73b0\u6ca1\u6709\u7528<\/del><\/p>\n\n\n\n<p>\u811a\u672c\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\u4f7f\u7528\u4e86\u5927\u91cf\u5f02\u6b65\u64cd\u4f5c\u6765\u8fdb\u884c\u5e76\u53d1\u4e0b\u8f7d\uff0c\u6211\u4e00\u5ea6\u4ee5\u4e3a\u662f\u8fd9\u4e2a\u6a21\u5757\u7684\u95ee\u9898\uff0c\u8c03\u8bd5\u4e86\u534a\u5929\u4e5f\u6539\u4e86\u8bb8\u591a\u4ee3\u7801\uff0c\u7ed3\u679c\u6d4b\u8bd5\u53d1\u73b0\u8fd9\u4e2a\u6a21\u5757\u81ea\u5df1\u8fd0\u884c\u7684\u65f6\u5019\u5e76\u4e0d\u4f1a\u51fa\u73b0\u968f\u673a\u81ea\u6740\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u90a3\u95ee\u9898\u5230\u5e95\u662f\u4ec0\u4e48\u5462\u2014\u2014<\/p>\n\n\n\n<p>\u5176\u5b9e\u662f\u56e0\u4e3a child process \u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684 maxBuffer\uff0c\u5f53 child process \u6253\u5370\u51fa\u7684\u65e5\u5fd7\u8d85\u8fc7 maxBuffer \u7684\u65f6\u5019\uff0c\u4f1a\u6536\u5230 SIGTERM \u3002<\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u968f\u673a\u51fa\u73b0\u5462\uff1a\u56e0\u4e3a\u6bcf\u6b21\u6253\u5370\u51fa\u7684\u65e5\u5fd7\u91cf\u4e0d\u4e00\u6837\uff0c\u6709\u7f51\u7edc\u9519\u8bef\u7684\u65f6\u5019\u6253\u5370\u51fa\u7684\u65e5\u5fd7\u4f1a\u66f4\u591a\uff0c\u4e5f\u5c31\u51fa\u73b0\u7684\u8d8a\u65e9\u3002\u800c\u4e14\u56e0\u4e3a\u51e0\u4e2a\u6a21\u5757\u90fd\u662f\u4f5c\u4e3a\u540c\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u8fd0\u884c\u7684\uff0c\u5404\u81ea\u6253\u5370\u7684\u65e5\u5fd7\u4f1a\u4e92\u76f8\u7d2f\u79ef\uff0c\u5bfc\u81f4\u4e86\u8fd9\u4e2a\u9519\u8bef\u4e0d\u4e00\u5b9a\u4f1a\u5728\u54ea\u4e2a\u6a21\u5757\u8fd0\u884c\u7684\u65f6\u5019\u88ab\u89e6\u53d1\u3002<\/p>\n\n\n\n<p>\u4f5c\u4e3a\u4e34\u65f6\u89e3\u51b3\u65b9\u6848\uff0c\u63d0\u9ad8\u4e86 maxBuffer \u7684\u503c\uff0c\u95ee\u9898\u89e3\u51b3\u3002<\/p>\n\n\n\n<p>References:<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/43401872\/node-js-child-process-exits-with-sigterm\">https:\/\/stackoverflow.com\/questions\/43401872\/node-js-child-process-exits-with-sigterm<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/39159312\/child-process-exits-with-code-null\">https:\/\/stackoverflow.com\/questions\/39159312\/child-process-exits-with-code-null<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u6bb5\u65f6\u95f4\u4f7f\u7528 Node \u5199\u4e86\u4e00\u4e9b\u5c0f\u5de5\u5177\u811a\u672c\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u547d\u4ee4\u6267\u884c\uff0c\u7ed3\u679c\u53d1\u73b0\uff0c\u5728\u8fd0\u884c\u8f83\u957f\u7684\u4efb\u52a1\u65f6\uff0c\u7ecf\u5e38\u4f1a\u53d1\u751f\u81ea &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/sora.sound.moe\/?p=958\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cJavaScript &#8211; \u968f\u673a\u81ea\u6740\uff1f\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"twentyseventeen-featured-image":false,"twentyseventeen-thumbnail-avatar":false},"uagb_author_info":{"display_name":"Sora","author_link":"https:\/\/sora.sound.moe\/?author=1"},"uagb_comment_info":0,"uagb_excerpt":"\u524d\u6bb5\u65f6\u95f4\u4f7f\u7528 Node \u5199\u4e86\u4e00\u4e9b\u5c0f\u5de5\u5177\u811a\u672c\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u547d\u4ee4\u6267\u884c\uff0c\u7ed3\u679c\u53d1\u73b0\uff0c\u5728\u8fd0\u884c\u8f83\u957f\u7684\u4efb\u52a1\u65f6\uff0c\u7ecf\u5e38\u4f1a\u53d1\u751f\u81ea&hellip;","_links":{"self":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts\/958"}],"collection":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=958"}],"version-history":[{"count":0,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts\/958\/revisions"}],"wp:attachment":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}