How to synchronise Angular http get?

0 votes

I can execute a method when the request is completed, but how can i wait till a http get is completed and return the response using in ng2 http?

getAllUser(): Array<UserDTO> {
    this.value = new Array<UserDTO>();
    this.http.get("MY_URL")
                    .map(res => res.json())
                    .subscribe(
                        data => this.value = data,
                        err => console.log(err),
                        () => console.log("Completed")
    );

    return this.value;
} 

the "value" will is null when its returned because get is async..

Sep 8 in Angular by kartik
• 35,500 points
42 views

1 answer to this question.

0 votes

Hello @kartik,

 the only way around this: wrap your call in a while loop with a flag. Don't let the code continue until that flag has "continue" value.

Pseudo code as follows:

let letsContinue = false;

//Call your Async Function
this.myAsyncFunc().subscribe(data => {
   letsContinue = true;
}; 

while (!letsContinue) {
   console.log('... log flooding.. while we wait..a setimeout might be better');
}

Hope it helps!!

Thank You!!

answered Sep 8 by Niroj
• 76,940 points

Related Questions In Angular

0 votes
1 answer

How to load external scripts dynamically in Angular?

Hello kartik, You can use following technique to ...READ MORE

answered Apr 22 in Angular by Niroj
• 76,940 points
12,618 views
0 votes
1 answer

How to get parameter value from query string?

Hello @kartik, Using component <Route path="/users/:id" component={UserPage}/> this.props.match.params.id The component is ...READ MORE

answered Jul 22 in Angular by Niroj
• 76,940 points
206 views
0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6 in Angular by Niroj
• 76,940 points
72 views
0 votes
1 answer

How to get current value of RxJS Subject or Observable?

Hello @kartik, A Subject or Observable doesn't have a current value. When ...READ MORE

answered Sep 8 in Angular by Niroj
• 76,940 points
432 views
0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13 in PHP by Niroj
• 76,940 points
201 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28 in Java-Script by Niroj
• 76,940 points
757 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27 in PHP by Niroj
• 76,940 points
176 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27 in PHP by Niroj
• 76,940 points
105 views
0 votes
1 answer

How to pass a string parameter from angular UI to node.js backend?

Hello Kartik, There are three ways to get ...READ MORE

answered Apr 22 in Angular by Niroj
• 76,940 points
2,072 views
0 votes
1 answer

How to detect a route change in Angular?

Hii Kartik For Angular 7 someone should write like: this.router.events.subscribe((event: Event) ...READ MORE

answered Apr 22 in Angular by Niroj
• 76,940 points
6,742 views