Named Arrow Functions
Nick Krempel
ndkrempel at google.com
Wed Aug 12 14:00:40 UTC 2015
On 12 August 2015 at 02:56, Isiah Meadows <isiahmeadows at gmail.com> wrote:
> ```js
>
> let p = new Promise((resolve, reject) =>
> setTimeout((x => () => x(x))(handler => {
> onNotNeeded(() => clearTimeout(handler));
>
> // `return` is to take advantage of TCO
> return doSomethingAsync(err => {
> if (err != null) return reject(err)
> else return resolve();
> });
> }));
> ```
>
This doesn't work as the function passed to clearTimeout does not === the
function passed to setTimeout.
In fact, they're not even behaviorally equal as the function passed to
setTimeout is expecting no parameters and the function passed to
clearTimeout is expecting one parameter - i.e. this is not even correct in
lambda calculus.
A lambda-calculus-correct version could be:
```
setTimeout((x=>(y=>()=>x(y(y)))(y=>()=>x(y(y))))(handler => {...}));
```
But this would still suffer from the object identity problem mentioned
above. A final JS-correct version could be:
```
setTimeout((x => {const y = () => x(y); return y;})(handler => {...}));
```
Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150812/2a3c1c35/attachment.html>
More information about the es-discuss
mailing list