Live data from Hacker News

Kal – a clean JavaScript alternative without callbacks

rzimmerman.github.io

101–110 of 130 posts

Re: Kal – a clean JavaScript alternative without callbacks

#101

This is a project I've been working on in my spare time for a while now and I've finally decided to throw it out there and get some feedback. Do you think the 'wait for' callback syntax is useful? What big features do you think are missing?

Are you aware of the ToffeeScript project? Can you make them a website and start promoting it? Because you are missing all if the features in ToffeeScript/CoffeeScripy and ToffesScript needs a shiny website because that's how people judge languages now is how shiny their website is apparently based on the fact that no one seems to care about ToffeeScript. BTW no I'm not the author of ToffeeScript.

Re: Kal – a clean JavaScript alternative without callbacks

#102
post #61

It's remarkable how similar this code looks to the monadic way of doing it. For this Kal code... task getUserFriends(userName) wait for user from db.users.findOne {name:userName} wait for friends from db.friends.find {userId:user.id} if user.type is 'power user' for parallel friend in friends wait for friendsOfFriend from db.friends.find friend for newFriend in friendsOfFriend friends.push newFriend unless newFriend…

If you are looking for a syntax closer to Haskell's, you can take a look at LiveScript ( http://livescript.net/ ) eg. getUserFriends = (userName) -> user Specifically, the section on "backcalls": http://livescript.net/#backcalls

If you are looking for Haskell, you can take a look at Fay (https://github.com/faylang/fay/wiki).

Re: Kal – a clean JavaScript alternative without callbacks

#103
post #47

Does anyone know if there is a JS dialect out there that adds support for callback-less async code without also adding a bunch of other coffeescripty syntax changes at the same time?

Try http://www.neilmix.com/narrativejs/doc/ -- "a small extension to the JavaScript language that enables blocking capabilities for asynchronous event callbacks".

Re: Kal – a clean JavaScript alternative without callbacks

#104
post #61

Earlier quoted context omitted.

If you are looking for a syntax closer to Haskell's, you can take a look at LiveScript ( http://livescript.net/ ) eg. getUserFriends = (userName) -> user Specifically, the section on "backcalls": http://livescript.net/#backcalls

If you are looking for Haskell, you can take a look at Fay ( https://github.com/faylang/fay/wiki ).

What about Haste? https://github.com/valderman/haste-compiler

Re: Kal – a clean JavaScript alternative without callbacks

#105
What do I do when I want to limit the number of tasks that are run in parallel? I'm talking about something like the queues in the famous async.js. I've seen tame.js trying to solve this by a construct named rendezvous but to me it looked even messier than inline callbacks.

Re: Kal – a clean JavaScript alternative without callbacks

#106
post #81

Earlier quoted context omitted.

What do you mean by "actually fix?"

Stop nesting anonymous functions. Un-nest them, name them, and encapsulate the process in a monad.

monad*

*jQuery or promises like API object, whereupon you place methods- some of which may be asynchronous. an asynchronous operation returns an object with a method that performs the next action and optionally takes a callback, passing in the results of the previous asynchronous operation as a value- thus flattening the callbacks out into a sequence instead of a nesting.

Constructed properly, you can create monad combinators/transformers, to do things like automatically wrap every step in your chain with exception handling. Using common JS promises libraries gives you error handling for free.

Re: Kal – a clean JavaScript alternative without callbacks

#107
I came across Galaxy yesterday, linked to from the callbacks/goto discussion. It's an NPM module that gives C# async/await semantics to JS/es6 code (of course it requires the harmony VM). It simply toggles function*/function on things, such as required modules, and provides some extra helpers... the author gives a really nice walkthrough:

https://github.com/bjouhier/galaxy

Galaxy does not claim to get rid of callbacks, though... only to help make writing JS/es6 with async/await semantics easier. I've yet to try it out, but the instructions seem clear.

Re: Kal – a clean JavaScript alternative without callbacks

#110
post #19

Excellent work. Especially useful since it appears to throw exceptions. That means it's a step up from Iced Coffeescript. The syntax is a little verbose and might be shortened to something like C#: `user = await db.users.findOne {name:userName}`. Would be a lot more clear to me.

As far as I can tell from reading the readme, it does NOT support throwing exceptions from within callbacks. Because, of course, there's no way to technically do that in anything that is JavaScript at its core -- at least as far as I'm aware of. Correct me if I'm wrong? It says "This includes error handling via callbacks." -- not error handling via exceptions. Elsewhere it says "Any errors reported by fs.readFile (re…

if you do something like:

  wait for data from fs.readfile 'test.txt'
  print data.toString
and readFile calls back with an error (the first argument is non-null), the error will be thrown just before the print statement. So print will not execute and you'll get a stack trace. The cool thing is that you can do these async calls within try blocks:

  try
    wait for data from fs.readfile 'test.txt'
    print data.toString()
    if data.length > 0
      wait for data2 from fs.readfile 'test2.txt'
      print data2.toString()
    else
      print 'too short'
  catch e
    print 'there was an error'
  wait for resp from db.save()
Will do what you expect - abort after either wait for if it fails and run the catch clause. Think about how you'd do that in JS:

  var error = null;
  var data, data2, resp;
  fs.readFile('test.txt', function (err, data) {
    if (err) return handleErr(err);
    console.log(data.toString())
    if (data.length > 0) {
      return fs.readFile('test2.txt', function (err, data2) {
        if (err) return handleErr(err);
        console.log(data2.toString());
        return closeout();
      });
    } else {
      console.log('too short');
      return closeout();
    }
  });
  function handleErr(err) {
    console.log('there was an error');
    return closeout();
  }
  function closeout() {
    return db.save(function (err, r) {
      if (err) throw err;
      resp = r;
    });
  }
Post reply on HN