I am using Parse.com in my project and I noticed something very weird that is preventing me from implementing what I want. This code:
(function() {
console.log('A');
Parse.Cloud.run('getCarMakes', {}, {
success: function(results) {
console.log('B');
for (var i = 0; i < results.length; i++) {
$scope.makes.push(results[i]);
}
},
error: function() {
console.log('C');
}
});
console.log('D');
for (var i = 0; i < $scope.makes.length; i++) {
console.log($scope.makes).get('Make');
}
})();
The console Output is: A B D C
How come D comes before C? What Can I do about it?
Parse.Cloud.runis a method that causes code to run asynchronously (request to the server), and is why you must provide callbacks forsuccess(runs when the request succeeds) anderror(runs when the request fails in some way). There is no fix, you need to think asynchronously and redesign your code in a way that works with that. Also, I'm not sure why/how you would get both B and C. Although not the same thing, this might help: stackoverflow.com/questions/14220321/…