Home:ALL Converter>How to use IcedCoffeeScript in function with two callbacks?

How to use IcedCoffeeScript in function with two callbacks?

Ask Time:2013-09-04T21:49:59         Author:franza

Json Formatter

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?

Author:franza,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18615721/how-to-use-icedcoffeescript-in-function-with-two-callbacks
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
yy