Let's assume I have such function (in Javascript):
function fun(success_cb, error_cb) {
var result;
try {
result = function_that_calculates_result();
success_cb(result);
} catch (e) {
error_cb(e);
}
}
And I use it like:
fun(function(result) {
console.log(result);
}, function(error) {
console.log(error.message);
});
How can I rewrite usage of this function in IcedCoffeeScript with await
and defer
?
saintmac :
I don't think there is an optimal way to do that in iced coffee script, although that post has some interesting suggestions: Iced coffee script with multiple callbacks\n\nI would just stick to vanilla coffee script:\n\nThis is how your function would be writtent in coffee-script\n\nfun = (success_cb, error_cb) ->\n try\n result = function_that_calculates_result()\n success_cb result\n catch e\n error_cb e\n\n\nand how you would call it in coffee script\n\nfun (result) ->\n console.log result\n, (error) ->\n console.log error.message\n\n\n\n\nIf you can rewrite the fun function in an \"errback\" style (err, result) in coffee script, that would be:\n\nfun = (callback) ->\n try\n result = function_that_calculates_result()\n callback null, result\n catch e\n callback e\n\n\nyou would then use it like that in iced coffeescript\n\nawait fun defer error, result\nif error\n console.log error.message\nelse\n console.log result\n",
2013-09-16T13:42:27