Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid an implicit "return" in coffeescript in conditional expressions?

I am implementing a function that has deferred value to return and within the function I have many nested conditional expressions:

e.g.:

deferred = Q.defer()
FS.readFile("foo.txt", "utf-8", (error, text) ->
    if error
      deferred.reject(new Error(error))
    else
      deferred.resolve(text)
)
return deferred.promise

which than will be compiled into:

var deferred;

deferred = Q.defer();

FS.readFile("foo.txt", "utf-8", function(error, text) {
  if (error) {
    --> return <-- deferred.reject(new Error(error));
  } else {
    --> return <-- deferred.resolve(text);
  }
});

return deferred.promise;

I need only the last return, but not the if/else returns (i.e. --> return <-- in the compiled code)

How can I avoid such a behavior (implicit returns where they are do not needed) of the coffeescript compiler?

like image 396
static Avatar asked Mar 18 '13 04:03

static


People also ask

What is a implicit return?

What is Implicit Return? A function is returned values without using the return keyword, it's called an implicit return. The Rules of Implicit Return. You must use an implicit return in a concise body.

Is CoffeeScript still a thing?

As of today, January 2020, CoffeeScript is completely dead on the market (though the GitHub repository is still kind of alive).


Video Answer


2 Answers

Coffeescript automatically returns the result of the last expressions, so if you don't want it to return the results of the if then you need to add another expressions. In this case, just add return.

FS.readFile "foo.txt", "utf-8", (error, text) ->
  if error
    deferred.reject new Error(error)
  else
    deferred.resolve text
  return

Also, error is already an Error object, so you can just reject it directly.

deferred.reject(error)
like image 130
loganfsmyth Avatar answered Oct 24 '22 08:10

loganfsmyth


You can't, exactly. You can either ignore them when not needed (which is the most common thing to do) or provide an explicit alternative by adding an additional statement at the end of the function. I think trying to do this all the time in your code base is fighting a war against the language you cannot win, so my personal recommendation is just accept Mr. Ashkenas's implicit return and go on your merry way.

fs.readFile "foo.txt", "utf-8", (error, text) ->

  # postfix style if statement here avoids the else
  # of course, the value returned you may not like, so 
  # you probably won't use this style, but my boilerplate for
  # error handling is
  # return callback(error) if error

  return deferred.reject(new Error(error)) if error
  deferred.resolve(text)

  # Here you can add an explicit return like
  return

  # or some other expression
  null

  # or 'this' in cases where chainability might be nice
  this

  # or so you don't accidentally delete this statement later thinking it is
  # useless
  return null

any of those forms will work, but in practice I don't see these commonly

like image 2
Peter Lyons Avatar answered Oct 24 '22 06:10

Peter Lyons