I can't seem to figure out the behavior of Angular's promises. Are promises actually asynchronous? I'm a bit confused about this concept.
When using promises in my code to handle a large process (such as reading and writing hundreds of big files), I noticed that the display seems to freeze, even though the code is wrapped inside a promise. This makes me think that it might not actually be asynchronous and could be blocking the main thread.
In the snippet below, which you can also view on this Plnkr, I am troubled by how to update the progress bar while the big process is running. I understand why the display freezes when the process is executed in the main thread, but I don't see why it's still freezing when using Angular's promises.
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $q) {
function hugeProcess () {
var i = 0;
var start = new Date().getTime();
while (i++ < 100000) {
console.log(i);
}
var end = new Date().getTime();
var time = end - start;
$scope.processTime = 'Done in ' + time + 'ms';
}
$scope.onClickStartHugeProcess = function () {
console.log('onClickStartHugeProcess');
hugeProcess();
};
$scope.onClickStartHugeProcessWithPromise = function () {
console.log('onClickStartHugeProcessWithPromise');
$q.when()
.then(function () {
return hugeProcess();
});
};
});