Imagine having a simple function like this:
foo ->
User.findById someId, (err, user) ->
return "hello #{user.name}"
If we translate it using coffeescript here's the result:
foo(function() {
return User.findById(someId, function(err, user) {
return "hello " + user.name;
});
});
In this code snippet, there are two returns for some reason. However, our goal is to only return "hello" after the callback.
The workaround I found was adding an extra return
at the end of the function to avoid returning the entire function itself:
foo ->
User.findById someId, (err, user) ->
return "hello #{user.name}"
return
With this modification, the translation becomes:
foo(function() {
User.findById(someId, function(err, user) {
return "hello " + user.name;
});
});
Do you know of a more efficient way to achieve the same result without needing to use an additional return
statement?