I have written the following function which is meant to return promise 
after the for-loop completes, but instead, it resolves straight away and 
results returns as an empty array. 
function fr(f, p) {
  var deferred = $q.defer();
  var promise = deferred.promise;
  var results = [];
  var callback = function(t, base64Img) {
    var u = base64Img;
    var c = f.data;
    var diff = resemble(u).compareTo(c).onComplete(function(result) {
      $log.debug('Comparing ' + t.name + ' with uploaded f');
      t.result = result;
      promise = promise.then(function() {
        results.push(t);
      });
    });
  };


  for (var i = 0; i < p.length; i++) {
    createBase64Property(p[i], callback);
  }


  deferred.resolve(results);
  return promise.then(function() {
    $log.debug(results);
    return results;
  });
}


function createBase64Property(t, callback, outputFormat) {
  var img = new Image();
  img.crossOrigin = 'Anonymous';
  img.onload = function() {
    var canvas = document.createElement('CANVAS');
    var ctx = canvas.getContext('2d');
    var dataURL;
    canvas.height = this.height;
    canvas.width = this.width;
    ctx.drawImage(this, 0, 0);
    dataURL = canvas.toDataURL(outputFormat);
    callback(t, dataURL);
  };
  img.src = t.imageUrl;


  if (img.complete || img.complete === undefined) {
    img.src = 
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
    img.src = t.imageUrl;
  }
}
});


-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to