Skip to main content

End a script early

  • September 30, 2020
  • 1 reply
  • 139 views

Forum|alt.badge.img+2

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.

This topic has been closed for replies.

1 reply

kuovonne
Forum|alt.badge.img+29
  • Brainy
  • September 30, 2020

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) {
    return;
  }
  // 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.