How to use the on function from process

Find comprehensive JavaScript process.on code examples handpicked from public code repositorys.

process.on is an event listener in Node.js that allows a program to listen for various types of events happening in the process.

376
377
378
379
380
381
382
383
384
385
let color = configDb.db("qx.default.color", null);
if (color) {
  let colorOn = consoleControl.color(color.split(" "));
  process.stdout.write(colorOn + consoleControl.eraseLine());
  let colorReset = consoleControl.color("reset");
  process.on("exit", () =>
    process.stdout.write(colorReset + consoleControl.eraseLine())
  );

  let Console = qx.tool.compiler.Console.getInstance();
fork icon257
star icon758
watch icon0

30
31
32
33
34
35
36
37
38
 */
const main = () => {
  process.title = ROVER_TITLE

  const controller = new Controller(merge({ isStandalone: IS_STANDALONE }, config.rovers.lsk))
  process.on('message', ({ message, payload }: { message: string, payload: string }) => {
    globalLog.debug(`Got message ${message} ${payload}`)
    controller.message(message, payload)
  })
fork icon22
star icon45
watch icon26

+ 3 other calls in file

How does process.on work?

process.on is a method provided by the Node.js runtime that allows a developer to register event handlers for various system events, such as uncaught exceptions, process exits, and system signals, among others. When a registered event is triggered, the corresponding handler function is executed, giving developers the ability to gracefully handle or recover from unexpected events in their Node.js applications.

13
14
15
16
17
18
19
20
21
22

// This runs the app in either CLI or GUI mode.
// We don't load the heavyweight Electron requirements unless
// we're running in GUI mode.
function run() {
    process.on('uncaughtException', function (error) {
        console.error(error);
        Context.logger.error(error);
    });
    let opts = minimist(process.argv.slice(2), {
fork icon2
star icon39
watch icon10

117
118
119
120
121
122
123
124
125
    detail: 'This is some additional information',
});

process.emit("warning", 100);

process.on('warning', (warning) => {
    warnings.push(warning);
    ev.set();
});
fork icon311
star icon0
watch icon0

Ai Example

1
2
3
process.on("exit", (code) => {
  console.log(`Process exited with code ${code}`);
});

This code sets up a listener for the "exit" event emitted by the Node.js process object, which is triggered when the process is about to exit, and logs a message to the console with the exit code of the process.

115
116
117
118
119
120
121
122
123
124
      console.log(`Worker ${worker.process.pid} died. Forking a new one...`)
      cluster.fork()
    }
  })
  // Graceful shutdown of all workers
  process.on('SIGTERM', gracefulClusterShutdown)
} else {
  run()
  console.log(`Worker ${process.pid} started`)
}
fork icon103
star icon0
watch icon27

91
92
93
94
95
96
97
98
99
100
        logger.log(`Electron Process closed. Exited with code ${code}`);
        // Server shouldn't shut down on exit because electron restart closes down electron and restarts in the background.
        // process.exit();
});

process.on('exit', () => {
        // Server shouldn't shut down on exit because electron restart closes down electron and restarts in the background.
        // electronProcess.kill();
        killElectronProcesses();
});
fork icon1
star icon11
watch icon9

38
39
40
41
42
43
44
45
46
47
48
49
  console.error('Unhandled rejection:', error);
});


async function main () {
  if (!ANALYZE_UNHANDELD_REJECTION)
    process.on('unhandledRejection', unhandledRejectionHandler);


  await require('dcp-client').init({
    configName: '../etc/dcp-config',
  });
fork icon0
star icon4
watch icon3

+ 11 other calls in file

79
80
81
82
83
84
85
86
87
88
89
90
function beginDownload(){
    console.log("<---[ Now downloading", filesQueue.length, "files ]--->");
    clearInterval(interval);
    downloadsOutOf = filesQueue.length;


    process.on("SIGINT", () => {
        const hostStr = "./" + new URL(process.argv[2]).host;


        if(!fs.existsSync(hostStr)) mkdirSync(hostStr);
        fs.writeFileSync(hostStr + "/_scraperInfo.txt", `Interrupted at ${((downloadCount/downloadsOutOf) * 100).toFixed(2)} % of downloads. That's ${downloadCount} out of ${downloadsOutOf} files.`);
fork icon0
star icon0
watch icon1

997
998
999
1000
1001
1002
1003
1004
1005
1006

if (!proc.killed) { // testing mainly to avoid leak warnings during unit tests with mocked spawn
  const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
  signals.forEach((signal) => {
    // @ts-ignore
    process.on(signal, () => {
      if (proc.killed === false && proc.exitCode === null) {
        proc.kill(signal);
      }
    });
fork icon0
star icon0
watch icon1

202
203
204
205
206
207
208
209
210
211
    else {
        // webpack 2 / 3
        this.compiler.plugin('watch-close', watchClose);
        this.compiler.plugin('done', done);
    }
    process.on('exit', function () {
        _this.killService();
    });
};
ForkTsCheckerWebpackPlugin.prototype.registerCustomHooks = function () {
fork icon0
star icon0
watch icon0

13
14
15
16
17
18
19
20
21
22
host.ElectronHost = class {

    constructor() {
        this._document = window.document;
        this._window = window;
        process.on('uncaughtException', (err) => {
            this.exception(err, true);
            this._terminate(err.message);
        });
        this._window.eval = global.eval = () => {
fork icon0
star icon0
watch icon290

+ 3 other calls in file