Discussion:
`throw` as an expression?
Axel Rauschmayer
2014-10-09 12:23:54 UTC
Permalink
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
axel at rauschma.de
rauschma.de



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141009/69c35bb5/attachment-0001.html>
Andrea Giammarchi
2014-10-09 13:35:50 UTC
Permalink
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
Post by Axel Rauschmayer
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
```js
asyncFunc()
.then(count => count >= 0 ? count : throw new Error(...))
.then(...)
.catch(...);
```
Could `throw` be turned into an expression?
--
Dr. Axel Rauschmayer
axel at rauschma.de
rauschma.de
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141009/3d2bf6db/attachment.html>
John Lenz
2014-10-09 20:57:59 UTC
Permalink
or a simple utility method


On Thu, Oct 9, 2014 at 6:35 AM, Andrea Giammarchi <
Post by Andrea Giammarchi
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
Post by Axel Rauschmayer
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
```js
asyncFunc()
.then(count => count >= 0 ? count : throw new Error(...))
.then(...)
.catch(...);
```
Could `throw` be turned into an expression?
--
Dr. Axel Rauschmayer
axel at rauschma.de
rauschma.de
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141009/dd0441ee/attachment.html>
Nathan Wall
2014-10-09 23:50:50 UTC
Permalink
Post by John Lenz
or a simple utility method
Promise.reject could be used.

```js
asyncFunc()
.then(count => count >= 0 ? count : Promise.reject(new Error(...)))
.then(...)
.catch(...)
```

Nathan
Brendan Eich
2014-10-09 23:50:42 UTC
Permalink
Post by John Lenz
or a simple utility method
Current keyword-anchored statement form does not require parens around
expression to evaluate and throw.

/be
John Lenz
2014-10-10 02:55:33 UTC
Permalink
Sure, but there is no pressing need here, "return" as an expression would
be more interesting. Not that I'm asking for that.
Post by Brendan Eich
Post by John Lenz
or a simple utility method
Current keyword-anchored statement form does not require parens around
expression to evaluate and throw.
/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141009/3718e8cc/attachment.html>
Isiah Meadows
2014-10-10 01:53:16 UTC
Permalink
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 <andrea.giammarchi at gmail.com>
To: Axel Rauschmayer <axel at rauschma.de>
Cc: es-discuss list <es-discuss at mozilla.org>
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
Post by Axel Rauschmayer
```js
asyncFunc()
.then(count => count >= 0 ? count : throw new Error(...))
.then(...)
.catch(...);
```
Could `throw` be turned into an expression?
--
Dr. Axel Rauschmayer
axel at rauschma.de
rauschma.de
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss
--
Isiah Meadows
Loading...