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?

2
  • 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: stackoverflow.com/questions/14220321/… Commented May 21, 2015 at 23:09
  • Why is this downmodded? That's lame. They obviously need help with async. Commented May 21, 2015 at 23:11

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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');
        }
    });

})();

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.