#694·choo

Magic in the emitter callback?

Author: crcdngCreated Mar 20, 2019Updated Mar 21, 2019

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?