Pavan
Pavan

Reputation: 25

AngularJS Service issue

Is it possible to call one first service and another?

I have two services, below are the details:

dataservice.getCPUUtilization(model.dbName).then(function (data) {
  model.cpuUtilizationChart = data;
  model.cpuPercentage = model.cpuUtilizationChart[0].combined;
  console.log ("******** CPU Utilization Chart components are ******* :" + model.cpuUtilizationChart);
  console.log ("******** CPU Percentage is ******* :" + model.cpuPercentage);                       
});
 dataservice.setCPUPercentage(model.setcpuPercentage);

After the getCPUUtilization service I need to call: But it is calling setCPUPercentage first and the getCPUUtilization?

Upvotes: 0

Views: 58

Answers (2)

alejandromav
alejandromav

Reputation: 943

More code should help, but i believe that these calls your're performing are async.

Making two calls in two consecutive lines of code doesn't that the last one will run right after the other ends.

You need to make the second call in the callback of the first one, that's the only way to ensure the first call is completed before making the second.

Try this:

dataservice.getCPUUtilization(model.dbName).then(function (data) {
    model.cpuUtilizationChart = data;
    model.cpuPercentage = model.cpuUtilizationChart[0].combined;
    console.log ("******** CPU Utilization Chart components are ******* :" + model.cpuUtilizationChart);
    console.log ("******** CPU Percentage is ******* :" + model.cpuPercentage);

    //Now in the callback
    dataservice.setCPUPercentage(model.setcpuPercentage);
});

Upvotes: 1

ppp
ppp

Reputation: 1

 dataservice.getCPUUtilization(model.dbName).then(function (data) {
  model.cpuUtilizationChart = data;
  model.cpuPercentage = model.cpuUtilizationChart[0].combined;
  console.log ("******** CPU Utilization Chart components are ******* :" + model.cpuUtilizationChart);
  console.log ("******** CPU Percentage is ******* :" + model.cpuPercentage);                       
}, function(){
 dataservice.setCPUPercentage(model.setcpuPercentage);
);

This would solve your problem.

Upvotes: 0

Related Questions