choojs / choojs/choo

Magic in the emitter callback?

Open
#694 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
6.8k
Forks
573
PR merge metrics
No merged PRs in 30d

Description

### Expected behavior
`choo` should accept valid JS code in the emitter callback.

### Actual behavior
`choo` throws

> "Unexpected token: punc ()) while parsing file: /app/index.js"

when an ES6 arrow function is used the emitter callback.

### Steps to reproduce behavior
This is working code (adapted from the intro tutorial, running on glitch)

```
const choo = require('choo');
const html = require('choo/html');
const main = require('./templates/main.js');

const app = choo();

app.use(function (state, emitter) {
state.animals = [
{ type: 'lion', x: 200, y: 100 },
{ type: 'crocodile', x: 50, y: 300 }
];

emitter.on('addAnimal', function () {
const obj = { type: 'lion', x: 100, y: 200 };
state.animals.push(obj);
emitter.emit('render');
});

});

app.route('/', main);
app.mount('div');
```

When I replace ` function () `with `() =>`

```
emitter.on('addAnimal', () => {
const obj = { type: 'lion', x: 100, y: 200 };
state.animals.push(obj);
emitter.emit('render');
});
```

`choo` throws the above error. Some kind of magic going on?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.