3 ms·
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 mi
by molf 13y ago
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.
- rzimmerman 13y agoDefinitely play with the exception handling. Latent bugs aside, asynchronous statements (wait fors) work in any crazy horrible nested combination of ifs, fors (parallel or series), and trys that you can think of. Take a look at the JS output with: kal -o output.js -f beautify input.kal to see the sausages being made. The http server demo is a good example (https://github.com/rzimmerman/kal/blob/master/examples/async-http-server.kal https://github.com/rzimmerman/kal/blob/master/examples/async...)
- tlb 13y agoA side-by-side line-by-line interactive display would be wonderful in writing Kal. Reading the JS explains what it's doing quite well, except that the Kal compiler uses way too many compound comma-separated statements instead of semicolons.
- rzimmerman 13y agoThat's actually a side effect of uglify.js's beautify, I believe. I'm going to try to get rid of that in upcoming releases.
- crazygringo 13y agoAs 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 (returned via callback) will be thrown automatically.", so maybe it converts certain callback error functions to exceptions? But in any case, that's not the same as throwing an exception inside of a callback, and having it "bubble up". Unless I'm misunderstanding (which would be great!).
- TheZenPsycho 13y agoYou can do it using monadic augmentation. Or aspect oriented programming. In plain english, make a function that takes two functions makeADoThingFunctionThatHandlesErrors(doThing, handleError) and returns a new version of the function that is internally wrapped in a try catch block with the handleError function getting called from the "catch" block. This is relatively straightforward to accomplish using closures/high order functions. This is one utility function you write once. Finally you can do this with a whole "monadic" api such as jquery- create a function that loops through all the methods on an object prototype and wraps them in an error handler, returns a new version of the prototype you can inherit from. Or instead of doing all that, you just use some library with promises (such as Q, or jQuery) that implements all the above, and all you need to do is write a single error handler function, and any error that gets thrown by any function in a promise chain gets caught and sent to that function.
- rzimmerman 13y agoif 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; }); }