我在每次循环迭代中运行一个函数,但是我需要确保在下一次迭代运行之前收到回调(成功)。当我运行下面的代码时,它运行循环,然后控制台显示之后的所有回调结果。
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;
};
调用外部函数:
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;
};
如果有更好的方法,我愿意接受有关其他方法的建议吗?
答案 0 :(得分:1)
异步操作无法暂停脚本的执行,但我们可以重构您的代码以避免使用deferred anti-pattern,而是使用$q#resolve
和$q#all
来处理所有数据和只有Promises的逻辑。
var getExerciseImages = function (exerciseImages) {
// Create a resolved Promise whose value is `exerciseImages` array
// Use 'when' in place of 'resolve' if using Angular
return $q.resolve(exerciseImages).then(function (arr) {
// Map each element to a Promise created by `convertImgToBase64URL`
// and wait for them all to resolve by wrapping the resulting
// array of Promises in `$q#all`
return $q.all(arr.map(function (url) {
var url = 'http://files.s3.amazonaws.com/medium/' + url + '.jpg'
return convertImgToBase64URL(url).then(function (result) {
return {
image: result,
fit: [200, 200]
};
});
}));
});
};
此方法的结果是Promise,其值是一个对象数组,每个对象都有两个属性image
和fit
:
var images = []; // An array of URLs
getExerciseImages(images).then(function (data) {
console.log(data); // => [{image, fit}, ...]
});
首先,在ES7中有async
functions可以await
。