ChenLee
ChenLee

Reputation: 1499

How to prevent node process exit in a callback?

For example if there is a callback function, after running the callback function, runprocess.exit()

function Test(callback) {
  callback();
  return process.exit();
}

How can I prevent process.exit() execute or prevent process exit in the callback function.

Upvotes: 1

Views: 114

Answers (1)

jabaa
jabaa

Reputation: 6827

You could throw an exception at the end of the callback function and catch it outside Test:

function Test(callback) {
  callback();
  return process.exit();
}

try {
  Test(() => {
    console.log('some code');
    throw 'Exception';
  });
} catch {}

console.log('process.exit() skipped');

Running example with exception : https://wandbox.org/permlink/UtDdMh4cHjUPURB8

Running example without exception: https://wandbox.org/permlink/Ih5L1PG1gJKMTJYN

Upvotes: 1

Related Questions