reasonml-community/bs-express

Understanding the call function on async callback

mfrachet opened this issue · 2 comments

Hello,

I'm from a JavaScript background and I m quite interested in Reason. I wanted to create my first Express server, and I'm facing some misunderstanding while reading the example codebase.

I'm stuck at the lines after : https://github.com/reasonml-community/bs-express/blob/master/example/Index.re#L389 :

let onListen = (port, e) =>
  switch e {
  | exception (Js.Exn.Error(e)) =>
    Js.log(e);
    Node.Process.exit(1)
  | _ => Js.log @@ "Listening at http://127.0.0.1:" ++ string_of_int(port)
  };

App.listen(app, ~onListen=onListen(3000), ());

From what I can read, onListen isn't asynchronous and the declared onListen function is called directly, like synchronously :

App.listen(app, ~onListen=onListen(3000), ());

In JavaScript (using the same syntax, omitting the "standard Expressjs APIs), we would have made something like :

App.listen(app, { onListen: onListen }, () => null); // or something like this, never mind :p

It's quite disturbing and I can't get the point out there. How does this work ?

And by the way, thank you for this awesome binding module ❤️

I think the part that is confusing you is the function currying in Reason.

onListen(3000) in Reason is equivalent to (e) => onListen(3000, e) in JavaScript.

See the guide for more information: https://reasonml.github.io/guide/language/function

Definitely need to give this project some documentation love :)