A
A
Adel Khalitov2019-02-15 17:19:27
JavaScript
Adel Khalitov, 2019-02-15 17:19:27

How to use await to wait for server response?

Good afternoon!
Tricky question.
There is a post request written in Angular, it is processed by the method of one service:

postHTTP(url, info: any) {
    var some: any;
    this.http.post(url, info, httpOptions).subscribe(
        data  => {
          some = data;
          console.log("POST Request is successful ", some);
        },
        error  => {
          console.log("Error", error);
        }
      );
      return some;
  }

Next, in the component, I do the following:
constructor(
    private myHttp: myHTTPService
    ) {  }
....
async pars2gisplease() {
      console.log( await this.myHttp.postHTTP('/pars2gis', {email: this.parser2gis}) );
    }

Everything would be fine, but the sequence of execution has already got me.
5c66c9fe9b235703803564.png
That is, the service receives data and can work with it, but how to pass it to the component is a problem.
My manipulations with async await do not work, I do this in order to use the returned information from the server in the component, and not in the service.
How can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2019-02-15
@SaveLolliPoP

postHTTP(url, info) {
  return new Promise((resolve, reject) => {
    this.http.post(url, info, httpOptions).subscribe((data) => {
      resolve(data);
      console.log("POST Request is successful ", data);
    }, (error) => {
      reject(error);
      console.log("Error", error);
    });
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question