代碼:#!/usr/bin/envnode//npminstallsync-promptvarprompt=require('sync-prompt').prompt;varname=prompt('whatisyourname?');console.log('Hello'+name);varfruit=prompt('whatisyourfavoritefruit?');console.log('metoo');process.on('SIGINT',function(){process.exit(0);});console.log('thiswouldbeprintafteryoutypeCTRL+C');process.exit(0);輸出結果:[honwhy@localhostnodejs]$./example.jswhatisyourname?honeyHellohoneywhatisyourfavoritefruit?^CmetoothiswouldbeprintafteryoutypeCTRL+C由于process.on是異步執行的,所以在程序退出前還會輸出metoo和thiswouldbe...等語句。有沒有辦法在程序(process)捕獲到SIGINT后同步方式執行,直接退出程序呢?UPDATE對原作者的模塊源碼做了一點修改了,提交了pullrequest,不過貌似提交失敗了,等待結果中。issue地址:https://github.com/shovon/sync-prompt/pull/9(2014年7月7日18:39:17)
[Node.js]有沒有辦法使得process捕獲到SIGINT信號程序直接退出?
繁花不似錦
2019-04-13 08:45:14