please wait

  • Jignesh Patel Mar-13-2019 07:09:27 AM ( 2 months ago )

    This is a pretty common and useful practice:

    // default via value
    var un = undefined
    var v1 = un || 1
    // default via a function call
    var myval = () => 1
    var v2 = un || myval()

    But it doesn't work (SyntaxError) when throwing an error:

    var v3 = un || throw new Error('un is not set!')

    Is there a way how to achieve the same effect in a similarly elegant way? This is IMHO a lot of boilerplate code:

    if (!un) {
        throw new Error('un is not set!')
    var v3 = un

    Or is there any theoretical obstruction, why this is not, and never will be, possible?

Please login

Similar Discussion

Recommended For You