Run the function solely once the asynchronous function has been executed

I need function F1() to wait for function F2() to fully execute and receive the response from a REST call in order to set some data.

Here is the code I attempted to use:

this.F1().subscribe(result => {
  this.F2(result);
})


    F1() {

      return this.graphDataService.getAvailableSpan(a, b).subscribe(res => {
        this.c = new Date(res.lastAvailableDate);
        return 1;
      }, error => {
        this.lastAvailableDate = new Date();
        return 1;
      })
    }

Answer №1

Take a look at this helpful article on How to implement multiple API calls in Angular functions

async apicall(){

      let data1 = await f1(); //ensure the first function returns promises
      let data2 = await f2(); //ensure the second function returns promises

    }

Answer №2

Here's one way to accomplish that:

async performAsyncRequest() {
        this.response = await this.httpClient.fetch(this.apiUrl).toPromise();
        this.processResponse(this.response);
    }

Answer №3

To introduce asynchronicity to the flow, consider utilizing RxJS operators. Implement the following approach:

ngOnInit() {
  this.loadData().subscribe(result => {
    this.processData(result);
  });
}

loadData(): Observable<any> {
  return this.dataService.fetchData(a, b).pipe(
    tap((res) => {
      this.timestamp = new Date(res.lastFetchDate);
    }),
    catchError((error) => {
      this.lastFetchDate = new Date();
      return of(error);
    })
  );
}

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

Leveraging a JSON file as a data repository for chart.js

I am struggling to incorporate JSON values into a bar chart. I have successfully logged the JSON data in the console, but I'm unsure how to include it in the data property for the chart. Below is the source JSON... {time: "2016-07-03T21:29:57.987Z" ...

Using jQuery to reference my custom attribute---"How to Use jQuery to reference My

Can you explain how to reference a tag using a custom attribute in jQuery? For example, if I have a tag like this: <a user="kasun" href="#" id="id1">Show More...</a> I want to reference the tag without using the id. So instead of using: $( ...

Tips for dynamically displaying Angular Material tags within an Angular component using JSON data

I received a JSON response structured like this: { _id: '5dd0d0dc4db1cf9c77781aaa', picture: 'http://placehold.it/32x32', name: 'Graciela Mcmahon', guid: '98c0fcc2-1dfc-4974-bdae-d8263d783e0a&ap ...

My initial junior UI/UX assignment: Can you confirm whether this form modal dialog is pixel-perfect, and offer any suggestions for improvements

Currently diving into my first project as a Junior UX/UI Designer. Coming from a background in software engineering, I decided to challenge myself by focusing on design. I'm seeking feedback on the pixel perfection of this modal window, which my seni ...

Tips for making a React/Socket.IO app accessible from external sources

Trying to get my React app (built with Node.js and Socket.IO) running locally and accessible from external sources has been a challenge for me. I've managed to expose port 3000 using ngrok tunneling, but my socket connection is listening on port 3001. ...

Is Your Website Sluggish because of an Excessive Amount of JavaScript on Page

After finally resolving some of the Javascript issues I was facing, I have streamlined my code to utilize just one library now, which is a huge improvement from how chaotic it was before. However, I have noticed a slight delay in the page load time, and I ...

Mastering Interpolation in React with TypeScript is essential for creating dynamic and interactive UI components. By leveraging the

Incorporating and distributing CSS objects through ChakraUI presents a simple need. Given that everything is inline, it seems the main issue revolves around "& > div". However, one of the TypeScript (TS) errors highlights an unexpected flagging of ...

Create PDF/A compliant documents using NodeJS

Current System Information I am currently utilizing an Angular application to collect user inputs, which are then sent to a NodeJS service to create a PDF document. The pdfmake package is being used in this process. What I Need The current package does n ...

Unlocking the Power of Data Passing Through Tabs

My Application Structure Client Module - Material Tab Tab 1 - Customer Table View Tab 2 - Edit Customer View <mat-tab-group> <mat-tab label="View"> <div> <app-customer-table></app-customer-table> & ...

What is the best way to send the value of this variable through the parameters?

In jQuery: initializeSliders(socket, '!{requestData}'); requestData is a special object (an HTTP request object in my Express.js web application) that contains information such as my session. However, when I pass this object into the initialize ...

Encountering ENOENT error when accessing view file in NodeJS and Express

I've encountered a strange issue with my nodeJS application after refactoring it. The app launches without any problems, the API responds correctly. However, when I attempt to access "/", I receive the following error: Error: ENOENT, stat '/view ...

The styles from the npm package are not being properly applied to the class

After creating my react npm package using webpack, I encountered an issue where the styles from the package were not being applied to classes when installed in my react project. I used style-loader in my webpack configuration, but kept getting errors sayin ...

Exporting multiple values from Angular to a CSV file

view image description here Hi there, In the CSV file, you'll notice that the values in the third column are enclosed in brackets and quotes. Here is the code I'm using for exporting: view image description here Thank you Is there a way to achie ...

Setting an action when clicking on a slice of a Doughnut chart using Chart.js

I have been working on integrating chart.js into my Django Project, and so far it has been going smoothly. I successfully created a doughnut chart with two slices. Now, I am trying to implement separate actions for each slice when clicked, such as redirect ...

[ERROR] There was a problem encountered during the execution of the ionic-app-scripts subprocess

I encountered an error while running my Ionic project. Below is the error message: [ERROR] ionic-app-scripts has unexpectedly closed (exit code 1). The Ionic CLI will exit. Please check any output above for error details. ionic3-firebase-shopping-car ...

Implement the addition of a class upon hovering using AngularJS

My goal is to include a new class when hovering over the li element using Angular in the code snippet below. <li ng-mouseenter="cola-selected=true" class="pull-left" ng-class="{'selected' : cola-selected}"> <a href="interna.html"> ...

How to Build a Custom Toolbar with Left and Right Aligned Elements using React.js and Material UI

Struggling with updating the toolbar on my website. Wanting the site name and logo on the left side, while login/sign-up buttons fixed to the right. Logo and title are in place, but can't get buttons to stay on right margin. Here's the code: func ...

Creating randomized sequences using JavaScript

One of my hobbies involves organizing an online ice hockey game league where teams from different conferences compete. It's important to me that every team gets an equal number of home and away matches throughout the season. To simplify this task, I&a ...

Interoperability between AngularDart and AngularJS

Discovering the Dart language and AngularDart after working with AngularJS has been exciting. However, my biggest concern is whether AngularDart supports all the amazing modules that AngularJS offers. I haven't been able to find any information on whe ...

Angular 2 Material 2 sleek top navigation bar

My toolbar is functional, however, there seems to be a gap around it instead of seamlessly blending at the top of the page. I also want it to be sticky, but I haven't figured out that part yet because it looks like my toolbar implementation is not co ...