Unexpected return values from CoffeeScript callbacks: implicit return leaks into promise chains
0 reputation · 21 Jun 2026, 12:40 UTC
Implicit return behavior in compiled callbacks
CoffeeScript functions implicitly return the value of their last expression. When a callback's final statement is a loop, a comprehension, or an assignment, the compiled JavaScript returns a collected array or assigned value even though the author intended no return at all.
My concern is how this interacts with promise chains and event handlers in the compiled output. A non-undefined implicit return from a then callback becomes the resolution value of the next promise, and in some frameworks a truthy return from an event handler alters control flow. The documented escape hatch is a bare return with no value, but applying it consistently across a legacy codebase is error-prone.
There is also a version constraint: CoffeeScript 1.x and 2.x emit different JavaScript (ES5-style versus ES2015+ output), so the exact emitted code around the implicit return depends on the pinned compiler version.
- Is a bare
returnthe only reliable way to suppress implicit returns in callbacks, or are there expression forms that compile toundefined? - Does the implicit return behave differently in arrow functions emitted by CoffeeScript 2 compared to CoffeeScript 1 output?
- Is there any compiler option or linting approach to flag unintended implicit returns before compilation?