$q.all(promisesArr) can help us with that.
Assume we have N methods that return promises: async1(), ..., asyncN().
The following code will log done only when all operations are resolved successfully.
function readOneFileAsDataURL (file, scope) { var deferred = $q.defer(); var reader = getReader(deferred, scope); reader.readAsDataURL(file); return deferred.promise; }; function readManyFileAsDataURL (files, scope){ var promises=[]; for(var i = 0 ; i < files.length; i ++){ promises.push(readOneFileAsDataURL(files[i], scope)); } return $q.all(promises); }; /* *Usage */ readManyFileAsDataURL(files, scope).then(function(allImages){ console.log(allImages.length); });
Result:
/* *Some log for more clear "begin read image : 0" "begin read image : 1" "begin read image : 2" "end read image : 2" "end read image : 0" "end read image : 1" * /
No comments:
Post a Comment