0

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?

Sushil
  • 2,837
  • 4
  • 21
  • 29
slashms
  • 928
  • 9
  • 26
  • 1
    I'm assuming `Parse.Cloud.run` is a method that causes code to run asynchronously (request to the server), and is why you must provide callbacks for `success` (runs when the request succeeds) and `error` (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: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Ian May 21 '15 at 23:09
  • Why is this downmodded? That's lame. They obviously need help with async. – Michael Cole May 21 '15 at 23:11

3 Answers3

2

Matansab, you'll need to start thinking "asynchronously".

The short answer is: Put "D" in the "success" callback.

(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]);
        }

        console.log('D');
        for (var i=0;i<$scope.makes.length;i++){
           console.log($scope.makes).get('Make');
        }
    },
    error: function () {
        console.log('C');
    }
});
    console.log("This will always run right after A");
})();

The "success" function is a "callback". Callbacks are a common pattern in Javascript. They are "called" after an async function returns. Async functions are requests to things like network or disk, that take so long Javascript shouldn't wait. Once the async function is ready, it calls the "callback".

You can learn more about javascript callbacks by googling, or just try here

Michael Cole
  • 15,473
  • 7
  • 79
  • 96
2

Michael Cole is right. Also depending on your implementation, since C is running in the error block, you can run D in there as well and it will run before C.

   (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('D');
        for (var i=0;i<$scope.makes.length;i++){
            console.log($scope.makes).get('Make');
        }

        console.log('C');
        }
    });

})();
Ronny K
  • 3,641
  • 4
  • 33
  • 43
0

it's not running sequentially because your success and error functions are callbacks. They're not executed until the Parse.com library actually invokes them.

Josh Kuhn
  • 176
  • 5