Problem with AngularJS Service

Can two different services be called in a specific order?

I am working with two services, here 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 calling the getCPUUtilization service I need to call: However, it seems like the setCPUPercentage service is being called before getCPUUtilization. What can I do about this?

Answer №1

Using more code could be beneficial, as it seems like the calls you are making are asynchronous.

Simply calling two functions in consecutive lines does not guarantee that the second one will execute immediately after the first one finishes.

To ensure that the first call is completed before moving on to the next one, you should make the second call within the callback function of the first call.

Here's an example:

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 make the second call within the callback
    dataservice.setCPUPercentage(model.setcpuPercentage);
});

Answer №2

 fetchData.fetchCPUData(model.dbName).then(function (information) {
  model.cpuData = information;
  model.cpuPercentageValue = model.cpuData[0].totalUsage;
  console.log ("******** CPU Data ******* :" + model.cpuData);
  console.log ("******** CPU Percentage Value ******* :" + model.cpuPercentageValue);                       
}, function(){
  fetchData.setCPUPercentageValue(model.setCpuPercentage);
);

This will address the issue at hand.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Exploring the Possibilities with WordPress and Waypoint Js

After exploring various resources and tutorials online, I am struggling to implement Waypoints with WordPress. Despite placing the necessary files like waypoints.min.js and waypoints.js in the designated download folder within the js directory of my templa ...

Adjust div size when the window size changes

I am facing an issue where the 5 divs fit well when the content is initially loaded, but if I resize the window, the size of the containers no longer align with the layout. For my layout, I am using Isotope and the following script to adjust the images to ...

Troubleshooting: Success with AJAX call in Chrome, but issues in IE

Having issues retrieving JSON data from a URL that displays the last 3 numbers of a webpage. The AJAX call functions correctly in Google Chrome but fails in Internet Explorer. I tried disabling caching using cache: false as suggested, but the problem persi ...

What is the best way to automatically redirect to a different page once a video has finished playing after 10 seconds?

I'm working on creating an HTML page that will automatically redirect after 10 seconds once a video has finished playing. The issue I'm facing is that the page is currently redirecting as soon as it loads, rather than waiting for the video to fin ...

Submitting req.body.variable_name using Angular

My goal is to successfully post req.body.variable_name. Below you can find my controller and services in Node.js. However, I am encountering an issue where I am receiving 'undefined' back in responseAddROBO. Can you help me identify the mistake? ...

DailyCodingChallenge: Discover two elements in an array that add up to a specified value

As someone who is relatively new to coding, I recently signed up for the daily coding problem mailing list and received the following question: If given a list of numbers and a specific number k, can you determine whether any two numbers from the list a ...

The outcome of the AJAX RSS Reader remains unpredictable

Utilizing the AJAX RSS Reader (visit this link) for extracting RSS data from my URL: http://vtv.vn/trong-nuoc.rss In my php file (readRSS.php): <?php $url = ("http://vtv.vn/trong-nuoc.rss"); $xmlDoc = new DOMDocument(); $xmlDoc->load($url); $ ...

IE Troubles: Timer Function Fails in Asp.Net MVC

I implemented the following code snippet: @Using Ajax.BeginForm("Index", New AjaxOptions() With { _ .UpdateTargetId = "AnswerSN", .HttpMethod = ...

A customizable JavaScript NPM Package designed to specifically escape XML special characters while preserving the integrity of the XML tags

Seeking recommendations for a JavaScript npm package that can escape the values in an XML document while preserving the XML tags. Ideally, looking for a solution that only allows letters A-Z (upper and lower case), digits 0-9, and spaces within the value ...

Engaging Floor Layout

Currently, I am in the process of developing an interactive floorplan for an office project. The main objective is to provide a user-friendly interface where individuals can search for a specific employee and have their respective office location highlight ...

Encountered an unexpected interpolation ({{}}) in column 3 of Data Bind RouterLink (Angular 2) that was expecting an expression

Encountering difficulties passing data to my routerLink. The goal is to change the route when the id reaches 4 within the ngFor loop. <input type="button" class="btn-cards" [ngClass]="getStyle(negociacao)" [routerLink]="['/{{negociacao.rota}}&apo ...

Update the text for the filter search placeholder in the Ant Table component

Is there a way to alter the default placeholder text in the Ant Table? I've set up a functioning example in documentation but couldn't find any prop for customization besides the customized filter dropdown, which I didn't want to implement. ...

Issues with JSON.parse in node.js on Windows 8.1 system

I've encountered an issue with the JSON.parse function in node.js on Windows, resulting in this error message in my command line: SyntaxError: Unexpected token ´╗┐ at Object.parse <native> at R:\filelocation\server\server.js ...

When attempting to submit the form, the page does not refresh as expected. The preventDefault() function also

I am facing an issue where, upon submitting a form, the page reloads. However, my requirement is to submit the form without reloading the page and send the data to a specific action URL. This may seem like a duplicate question but here are the details: F ...

Updating new objects in Angular using JavaScript Object-Oriented Programming may not be working

Recently delving into OOP in JavaScript while working on an AngularJS website, I encountered a situation where my object methods were only altering the properties within the class, but not affecting the new object itself. //Class Var Item = function() { ...

Guide to illustrating the connections between origin and destination nations utilizing IP addresses

My goal is to create an interactive world map with a clickable marker for each country. When a user clicks on a source country's marker, I want to display interactions with other countries in an aggregated manner. While I have successfully drawn the w ...

Bluebird Performing a series of promises in an array

I'm facing an issue while attempting to execute the following function. Create a file Send an email with the file attached Delete the file Despite my code below, when I check the received email, the file seems to have content that is not found. ...

When using a callback function in the Array Map method with only one argument, the function argument is returned as the index instead of the item

I'm experiencing an unexpected issue where the callback function of the map method in an array is receiving the index number as the first argument instead of the item itself. Interestingly, when I provide two arguments, the function behaves correctly ...

unable to retrieve the city name through geographical location information

I am currently using the following code snippet to retrieve the country and city names: <?php $user_ip= getenv('REMOTE_ADDR'); $geo= unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip")); $city= $geo["ge ...

"Experience the convenience of navigating through two slideshows using the user-friendly Easy Slider

I have implemented the Easy Slider 1.7 from on my website for creating slideshows. Everything works perfectly when there is only one slideshow on the page. However, I need to have two separate slideshows on the website located at different places. When I ...