News for hackers. Stuff that matters.
Re:Show HN: Callbaxx.js – A utility library to bring callbac... by z3t4 on Monday October 15, 2018 @06:10PM (#18224312)
Ignoring that the code doesn't handle any errors, there's really nothing wrong with nesting, as you can clearly see the code flow and understand what it does. The example (1) would look much better using Promise's as it's a chain of events. But imagine a horizontally scaled distributed system where each math operation run's on a load balanced cluster, you would need code to handle back-pressure, retry when errors occur, etc.

Here's how FizzBuzz might look like:

  function fizzBuzzLogic(nr, multipleOfThree, multipleOfFive) {
    var print = console.log;
    if(multipleOfThree && multipleOfFive) print("nr=" + nr + " FizzBuzz");
    else if(multipleOfThree) print("nr=" + nr + " Fizz");
    else if(multipleOfFive) print("nr=" + nr + " Buzz");
    else print("nr=" + nr);
  }

  function fizzbuzz(cb) {
    var modulo = require("modulo");
    var errors = [];
    var maxErrors = 3;
    var maxConcurrency = 5;
    var inProgress  = 0;
    var fizzBuzzCounter = 1;

    next();
    
    function next() {
      while(cb && inProgress < maxConcurrency && fizzBuzzCounter < 100) run(++fizzBuzzCounter);
    }
    
    function run(nr) {
      console.log("run: nr=" + nr);
      var multipleOfThree = undefined;
      var multipleOfFive = undefined;
      
      moduloTest(3);
      moduloTest(5);
      
      function moduloTest(multipleOf) {
        inProgress++;
        modulo(nr, multipleOf, moduloResult);
        
        function moduloResult(err, moduloNr) {
          inProgress--;
          if(err) {
            errors.push(err);
            if(errors.length >= maxErrors) {
              if(cb) cb(errors.join("\n"));
              cb = null;
            }
            else if(cb) moduloTest(multipleOf);
          }
          else {
            
            if(multipleOf==3) multipleOfThree = (moduloNr==0);
            if(multipleOf==5) multipleOfFive = (moduloNr==0);
            
            if(multipleOfThree != undefined && multipleOfFive != undefined) {
              fizzBuzzLogic(nr, multipleOfThree, multipleOfFive);
              next();
            }
          }
        }
      }
    }
  }
I haven't polished or run the code, it for example doesn't call the cb (which is a common error in callback based code), but you get the idea. As an exercise you could rewrite it using async *function or using Promises, but I believe the benefit of Promises wouldn't be as apparent.

There are no comments on this comment yet.