early exit from Nashorn script?

Tom Scavo trscavo at gmail.com
Tue Jan 28 09:18:00 EST 2020


On Mon, Jan 27, 2020 at 7:00 PM Jim Fox <fox at washington.edu> wrote:
>
> Every Nashorn script I've seen ends with its return value, e.g.,
> "nextFlow;" in mfa scripts.  Is there a way to exit with a value in the
> middle of the script?
>
> e.g.
>
>    # I know user cannot complete the login
>    exit("authn/you-are-toast");

This is a dev question but...

JavaScript embraces multiple styles of programming (which is both a
blessing and a curse). In this case, you probably want to adopt the
functional style and write a self-executing anonymous function [1]
whose inputs are the inputs to the script. A JavaScript function may
have multiple return statements so you can short-circuit function
execution under various conditions as desired.

There are examples of self-executing anonymous functions in the wiki.

Cheers,

Tom

[1] http://markdalgleish.com/2011/03/self-executing-anonymous-functions/


More information about the users mailing list