You are reading a single comment by @ChrisB and its replies. Click here to read the full conversation.
  • Used to work with node.js Async library and like this library.
    Having to do lot of asynchronous (so with callback) chaining functions in my project,
    such library might miss in Espruino.
    Example of async.js library transfered :

    function async() {  
      var only_once = function(fn) {
        var called = false;
        return function() {
          if (called) throw new Error("Callback was already called.");
          called = true;
          fn.apply(root, arguments);
        };
      };  
      
      var doSeries = function (fn) {
        return function () {
          var args = Array.prototype.slice.call(arguments);
          return fn.apply(null, [async.eachSeries].concat(args));
        };
      };  
    
      var eachSeries = function (arr, iterator, callback) {
        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 mapSeries = doSeries(_asyncMap);
      
      this.series = function (tasks, callback) {
        callback = callback || function () {};
        if (tasks.constructor === Array) {
          async.mapSeries(tasks, function (fn, callback) {
            if (fn) {
              fn(function (err) {
                var args = Array.prototype.slice.call(arguments, 1);
                if (args.length <= 1) {
                  args = args[0];
                }
                callback.call(null, err, args);
              });
            }
          }, callback);
        }
      };
    }
    
    var async = new async();
    
    async.series([
        function(done) {
          getImage(function(err, result) {
            //some logic)
            done(err, result); 
          });
        },
        function(done) {
          image2Tz(function(err, result) {
            //some ogic
            done(err, result);
          });      
        },
        function(done) {
          fingerFastSearch(function(err, result) {
            //some logic
            done(err, result);
          });
        }
      ],
      function(err, result) {
        if(err !== null) {
          console.log('Error : ' + err);
          //do logic
          return;
        }
                   
        console.log(result);
      }
    );
    

    If something similar is already present in Espruino, let me know...
    Any question or feedback, please let me know...
    I planned to use functions around control flow for the moment.

    [EDITED]
    PS : Sorry.... :(
    the code is not working so well..... I'll update it once fully working

About

Avatar for ChrisB @ChrisB started