1

I'm writing an Angular app. I have a service that makes an http request like this:

export class PingService {

  constructor(
    private http: HttpClient) { }

  ping() {
    return this.http.get<{ip, type}>(this.apiRoutes.pingService);
  }
}

I call this service inside a component:

login(credentials) {
    this.invalidLogin = false;

    this.pingService.ping()
      .subscribe(data => {
        this.apiConfigService.changeNetwork(data.type);
      },
      error => {
        throw(error);
      });

    // MORE THINGS TO DO //

I want to wait for the pingService.ping() to finish before executing the // MORE THINGS TO DO //. How can I wait for the service to finish?

3
  • 1
    The more things to do should be inside subscribe then.. Commented Jun 4, 2018 at 9:11
  • 2
    Move MORE THINGS TO DO inside the subscribtion of the service method Commented Jun 4, 2018 at 9:12
  • I know I can do that, but I want to make the ping() call synchronous. Is that possible? Commented Jun 4, 2018 at 9:14

4 Answers 4

2
login(credentials) {
    this.invalidLogin = false;

    this.pingService.ping()
      .subscribe(data => {
        this.apiConfigService.changeNetwork(data.type);
        this.doMoreThings();
      },
      error => {
        throw(error);
      });
}

doMoreThings() {
  console.log('I am doing more things');
}
Sign up to request clarification or add additional context in comments.

5 Comments

Is there no way to make the ping() call synchronous?
@trichetriche don't you mean synchronous?
No, I mean asynchronous. The code doesn't wait for ping to finish before running the next piece of code, hence asynchronous.
But isn't it not waiting for this.pingService.ping().subscribe to finish before running whatever is inside? Hence synchronous?
Inside yes, after it no. OP was trying to run code after the call, and not inside it. In my solution, it is the opposite. But anyway, I get your point, I guess that we just misunderstood each other !
1
login(credentials) {
this.invalidLogin = false;

this.pingService.ping()
  .subscribe(data => {
    this.apiConfigService.changeNetwork(data.type);

    --> To here // MORE THINGS TO DO //
  },
  error => {
    throw(error);
  });

--> Move this    // MORE THINGS TO DO //

2 Comments

Is there no way to make the ping() call synchronous?
It's already synchronous, it's waiting for the function to be finished before running whatever is inside the .subscribe() function.
0

Synchronous is not recommended with Javascript, especially when you are doing http requests. The way guys wrote already is the best way to do it.

Comments

0

I'd like to add something regarding other answers.

Most of the time you shouldn't "wait for http requests" in any JavaScript application as it may block the UI. Imagine your user does some action on your app while there's a connection loss or bad network. It would result in the UI being blocked until the request ends or times out, which is really bad experience.

1 Comment

Synchronous requests exist for a reason : don't say never, but rather most of the time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.