I'm running a function on each loop iteration, however I need to ensure that I receive the callback (success) before the next iteration runs. When I run the code below it runs the loops and then the console shows all the callback results after.
var getExerciseImages = function(exerciseImages) {
    var deferred = $q.defer();
    var imageColumns = [];
    var imageObject = {};
    for (var i = 0; i < exerciseImages.length; i++) {
        var url = 'http://files.s3.amazonaws.com/medium/' + exerciseImages[i] + '.jpg'
        convertImgToBase64URL(url).then(function(result) {
            imageObject = {
                image: result,
                fit: [200, 200]
            };
            imageColumns.push(imageObject);
        });
    };
    deferred.resolve(imageColumns);
    return deferred.promise;
};
Calling the outer function:
function convertImgToBase64URL(url, callback, outputFormat) {
    var deferred = $q.defer();
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function() {
        var canvas = document.createElement('CANVAS'),
            ctx = canvas.getContext('2d'),
            dataURL;
        canvas.height = this.height;
        canvas.width = this.width;
        ctx.drawImage(this, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);
        // callback(dataURL);
        deferred.resolve(dataURL);
        canvas = null;
    };
    img.src = url;
    return deferred.promise;
};
I am open to suggestions on other ways to do this if there is a better way?
 
    