0
for (var i = 0; i < pricingPlans.length; i++) {
    productServices.retrivePricingPlan(pricingPlans[i].Id.Value).then(function (objPricingPlan) {
        productServices.createPricingPlan(objPricingPlan.data).then(function (objNewPricingPlan) {
            var newPlanID = objNewPricingPlan.data.PricingPlan.Id.Value;
            console.log("New ID");
            console.log(newPlanID);
            console.log("Old ID");
            console.log(product.PricingPlanAssociations[i].PricingPlanId.value);
            //  product.PricingPlanAssociations[i].PricingPlanId.value = newPlanID
        });
    });
}

I am making REST calls inside the for loop, but I want the REST calls to execute in following order:

  1. Retrieve pricing plan[i]
  2. Create pricing plan[i]
  3. Create product[i]

but when I look at the console tab they are executing in a different order

How can I ensure the pricing plans are executed in that particular order inside the for loop?

NOTE: retrivePricingPlan and createPricingPlan return calls to $http.post().

Network Tab

2

1 Answer 1

1

Because the AJAX calls are all asynchronous, and the for loop is synchronous, the actual calls can be in any order. The "dependent" calls will be in the correct order, though. (That is, the promise chain will always call Retrieve before calling Create)

If you really need these to work in order, you can create a queue to manage the asynchronous call order.

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

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.