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.
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.
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!).
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.
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;
});
}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...)