Wednesday 15 July 2015

node.js - coffeescript : defining callbacks before they're called -


I am writing a basic node in coffeescript.js web server. When I write:

  server.listen (3000, listeners) listener = () - & gt; Console.log 'listening server on port 3000'   

This server starts, but the message does not print, so I collect that callback is not being called. On the other hand, when I do:

  listener = () - & gt; Console.log 'server listening port 3000' server.listen (3000, listener)   

Message has been printed on the console.

Why is the callback defined before the phone, but if it is defined later, why not?

The way the coffee script function definitions work, your first snippet is similar to a javascript in this way. Is:

  var message; Console.log (message ()); // Message is undefined at this point Message = function () {Return "Hello World!"; };   

Message is not present, when it is first accessed, so it throws an error as it seems you want:

  console.log (Message ()); Function Message () {Return "Hello World!"; }   

Which works fine, but AFAIK, there is no way to write that in the coffee script.

No comments:

Post a Comment