You are reading a single comment by @ChrisB and its replies. Click here to read the full conversation.
  • Hi Gordon,
    I used jsfiddle.net in Google Chrome (for the console)
    to test and compare the original code of async library.
    Here is the modified code with comments of portion of code that do not react the same in Espruino. I do not know what should work or not in Espruino, what should be standard or not, but async.js is normally using standard javascript (I think :) ).

    function async() {
    
      var eachSeries = function (arr, iterator, callback) {
    
        //Commented next statement as it fails. callback is always assigned to function () {};
        //even if callback has an value.
        
        //callback = callback || function () {};
    
        if (!arr.length) {
          return callback();
        }
        var completed = 0;
        var iterate = function () {
          iterator(arr[completed], function (err) {
            if (err) {
              callback(err);
              callback = function () {};
            }
            else {
              completed += 1;
              if (completed >= arr.length) {
                callback(null);
              }
              else {
                iterate();
              }
            }
          });
        };
        iterate();
      };
    
      var _map = function (arr, iterator) {
        //commented if(arr.map)fails and never enter the if even if arr has a value
        //if (arr.map) {
          return arr.map(iterator);
        //}
        //this is th original code when previous if is not commented
        //var results = [];
        //_each(arr, function (x, i, a) {
        //  results.push(iterator(x, i, a));
        //});
        //return results;
      };
    
      var _asyncMap = function (eachfn, arr, iterator, callback) {
        var results = [];
        arr = _map(arr, function (x, i) {
          return {index: i, value: x};
        });
        eachfn(arr, function (x, callback) {
          iterator(x.value, function (err, v) {
            results[x.index] = v;
            callback(err);
          });
        }, function (err) {
          callback(err, results);
        });
      };
    
      var doSeries = function (fn) {
        //original code
        //return function () {
        //   var args = Array.prototype.slice.call(arguments);
        //   return fn.apply(null, [async.eachSeries].concat(args));
        //};
    
        return function () {
          //arguments.slice replace Array.prototype.slice.call
          var args = arguments.slice(0);
    
          //the following statements replace [eachSeries].concat(args)
          //as .concat is not recognized
          var r = [eachSeries];
    
          for(var i in args) {
            //i should be the value of args and not an integer 
            //i is equal to 0,1,2 (args has 3 items) and normally should be 
            //the value of args[i], isn't it ???
            r.push(args[i]);
          }
          return fn.apply(null, r);
        };
      };
    
      var mapSeries = doSeries(_asyncMap);
    
      this.series = function (tasks, callback) {
    
        //even if callback has a value, the following line fails
        //callback = callback || function () {};
    
        mapSeries(tasks, function (fn, callback) {
    
          // statement if(fn) fails even if fn has a value, so updated it to if (fn !=== null)
          if (fn !== null) {
              fn(function (err) {
                var args = arguments.slice(1);
                if (args.length <= 1) {
                  args = args[0];
                }
                callback.call(null, err, args);
              });
          }
        }, callback);
      };
    
    }
    
About

Avatar for ChrisB @ChrisB started