Reputation: 4589
I've come across a use of functions in JavaScript that I can't seem to convert, correctly, to CoffeeScript syntax. I'd appreciate it if someone could let me know how to do this:
JavaScript version:
node.addEventListener('mousedown', () => {
setTimeout(() => {
console.log('pressed');
return;
}, 3000);
});
The function setTimeout expects 2 parameters - a function and a duration in milliseconds. Here's my first attempt at converting to CoffeeScript:
node.addEventListener 'mousedown', () =>
setTimeout () =>
console.log 'pressed'
return, 3000
return
This just gives me an error "unexpected comma" on line 4. So, I tried putting the duration on a separate line but, depending on whether I indent it one level or two, it just becomes part of the function code of either the function being passed to node.addEventListener (2nd parameter) or the function being passed to setTimeout (1st parameter).
Upvotes: 0
Views: 40
Reputation: 222398
You need to move both the comma and 3000 to a new line. The following code:
node.addEventListener 'mousedown', () =>
setTimeout () =>
console.log 'pressed'
return
, 3000
return
compiles to
node.addEventListener('mousedown', () => {
setTimeout(() => {
console.log('pressed');
}, 3000);
});
Tested here.
Another option is to wrap the inner function in parentheses:
node.addEventListener 'mousedown', () =>
setTimeout (() =>
console.log 'pressed'
return), 3000
return
Upvotes: 1