End a script early

Is it possible to end a script early? It seems like the process.exit() function isn’t available. I’d like to show an error message and exit if the input meets certain criteria.

I like to put the main functionality of a script inside a main function. If I want to end the script early, I use a return statement inside the script.

await main()
async function main() {
  // pseudo code for guard clause
  if (no data) {
  // main script functionality

Almost all of my scripts have some sort of async call, so my main function almost always needs await/async. If your script doesn’t use any async calls, you can remove them.

This thread also has a discussion about using a “top-level” return, but it is not recommended.

1 Like