Pavan Pavan - 4 months ago 31
AngularJS Question

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
?

Answer

More code should help, but i believe that two calls are async, so there's no garantee one will run after the other if you call both service in two consecutive lines of code. 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);
});