I JavaScript, has a spread-like operator for optional elements been considered? Something like:

['a', ?'b']
=> ['a', 'b']

['a', ?null]
=> ['a']

I know you can emulate this with a spread + conditional + array literal, but I think this could be way simpler to understand.

@rauschma or any other JavaScript experts

@acjay @rauschma Combined with Promise.all() you could have a nice conditional set of parallel tasks!

@aismallard @rauschma I agree. Something like this?

Promise.all([
? condition1 ? task1() : null,
? condition2 ? task2() : null,
? condition3 ? task3() : null,
])

@acjay @rauschma Hmm, with a ternary I think it's a bit confusing, maybe "?(condition ? task() : null)" instead? I wonder what syntactical issues ?expr yields.

Follow

@aismallard yeah, it's possible it might have to be parenthesized. I think there's some reason to think it could be workable tho since this would always be in the context of a list item, either after the opening `[` or a `,`.

As mentioned in my other comment, if you had prefix &, you'd have

Promise.all([
& condition1 && task1()
& condition2 && task2()
& condition3 && task3()
])

which is even less noisy.

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.