Here’s what I want to see:
kango.invokeAsync('kango.storage.getItem', 'item1', function(returned1) {
kango.invokeAsync('kango.storage.getItem', 'item2', function(returned2) {
alert(returned1 + returned2);
});
});
Here’s what I wrote in coffeescript:
kango.invokeAsync 'kango.storage.getItem', 'item1', (returned1) ->
kango.invokeAsync 'kango.storage.getItem', 'item2', (returned2) ->
alert returned1 + returned2
The problem here is that no matter what, coffeescript is making the function ()-> return something. In that case, the last statement is being returned for some reason.
If I were to place a second alert in the nested function with returned2, it would return instead of the first one:
kango.invokeAsync('kango.storage.getItem', 'item1', function(returned1) {
kango.invokeAsync('kango.storage.getItem', 'item2', function(returned2) {
alert(returned1 + returned2);
return alert('something');
});
How to have it avoid doing the return?
If you don’t want a function to return something then just say
return:returnbehaves the same in CoffeeScript as it does in JavaScript so you can sayreturnif you don’t want any particular return value.If you don’t specify an explicit return value using
return, a CoffeeScript function will return the value of the last expression so your CoffeeScript is equivalent to:The result will be the same though,
alertdoesn’t return anything so:will give
undefinedinxbut so will this: