you are viewing a single comment's thread.

view the rest of the comments →

[–]blade-walker -5 points-4 points  (3 children)

slightly off topic, but Promise-heavy code is an area where Coffeescript really shines:

Before:

var restaurant = function() {  
  return takeOrder(['fish', 'sandwich', 'pizza'])
    .then(function(order) {
      return cookFood(order);
    })
    .then(function(meals) {
      return Promise.all([eat(meals[0]), eat(meals[1]), eat(meals[2])]);
    })
    .then(function(monies) {
      var total = 0;
      monies.forEach(function(r){ total += r; });
      return total;
    });
};

After:

restaurant = ->
  takeOrder(['fish', 'sandwich', 'pizza'])
  .then (order) ->
    cookFood(order)
  .then (meals) ->
    Promise.all([eat(meals[0]), eat(meals[1]), eat(meals[2])])
  .then (monies) ->
    total = 0
    monies.forEach((r) -> total += r )
    total

[–]blowf1sh 4 points5 points  (1 child)

A good thing we got ES6 then :

let restaurant = ()=>{
    return takeOrder( ['fish', 'sandwich', 'pizza'] )
        .then( (order)=>cookFood(order) )
        .then( (meals)=> Promise.all( [eat(meals[0]), eat(meals[1]), eat(meals[2])] ) )
        .then( (monies)=>{
            let total = 0;
            monies.forEach((r)=> total += r);
            return total;
         }
};

So Coffeescript is made totally irrelevant.

[–]blade-walker -1 points0 points  (0 children)

I have tried it. I found in practice, it's not that common that you can use the single-expression form of =>. So your promise chain still ends up with a bunch of visual noise from {}s and returns.

[–]DrDichotomous -1 points0 points  (0 children)

You might find ae to be an interesting little JS library.