It would be convenient for throw to be one. Maybe an idea would be to
treat it similar to `void` or `delete`? If it were an expression, it
could be parsed very similarly. Here's a potential use case even
without all the other ES6 additions.
```js
function foo(bar) {
bar == null && throw new TypeError();
// do stuff with required parameter `bar`
}
// Currently, it requires this:
function foo(bar) {
if (bar == null) {
throw new TypeError();
}
// do stuff with required parameter `bar`
}
// Or, this: (totally stealing your hack, Andrea... :-P)
const foo = bar => bar == null ?
()=>{throw new TypeError()} :
undefined; /* do something */
```
It wouldn't surprise me if this becomes included eventually in, say,
Underscore/Lodash/etc. if not made an expression. It would just end up
easier to type `_.throw(new TypeError())`.
To clarify, here's what I mean by treating it like `void` and `delete`:
```js
void foo(); //=> returns undefined
delete bar[0]; //=> returns if the entry/property no longer exists
throw baz; //=> returns nothing/undefined (for sake of spec)?
```
> From: Andrea Giammarchi <[email protected]>
> To: Axel Rauschmayer <[email protected]>
> Cc: es-discuss list <[email protected]>
> Date: Thu, 9 Oct 2014 14:35:50 +0100
> Subject: Re: `throw` as an expression?
> probably not exactly the fat arrow usage you were looking for ... but it
> makes it trivial to inline any sort of block
>
> ```js
> asyncFunc()
> .then(count => count >= 0 ? count : ()=>{throw new Error(...)}())
> .then(...)
> .catch(...);
> ```
>
> Regards
>
>
> On Thu, Oct 9, 2014 at 1:23 PM, Axel Rauschmayer <[email protected]> wrote:
>>
>> Use case: With promises, the expression body form of arrow functions is so
>> convenient. Alas, `throw` being a statement, you can’t use it there. For
>> example, the following code is not syntactically legal:
>>
>> ```js
>> asyncFunc()
>> .then(count => count >= 0 ? count : throw new Error(...))
>> .then(...)
>> .catch(...);
>> ```
>>
>> Could `throw` be turned into an expression?
>>
>> --
>> Dr. Axel Rauschmayer
>> [email protected]
>> rauschma.de
>>
>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> [email protected]
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
>
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
--
Isiah Meadows
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss