1

While waiting for a response from express.js in the code below, Angular keeps waiting even though other GET requests have been completed and does not draw the relevant HTML at all.

Is there a way to avoid waiting for the following request response?

I checked this question , but it didn't work for me.

Angular:

ngOnInit() {
    this.requestToServer();
}

requestToServer():void{

  const httpOptions = {
    headers: new HttpHeaders({ 
      'Content-Type': 'application/json'
      })
  };

  const body = null;
  const url = `dummy`;
  this.http.post(url, body, httpOptions).subscribe();
}

Express.js:

app.post('/dummy', function (req, res, next) {

    // Even if you return this response to a client here, the subsequent process will continue. 
    res.status(200).json({
        completed: true
    });

    // Testing : I don't want Angular to wait here.
    sleep(5000);

    // A process with no response. (e.g.) db update query
    // ...
});
function sleep(waitMsec) {
    var startMsec = new Date();
    while (new Date() - startMsec < waitMsec);
}
Zalve
  • 95
  • 1
  • 10
  • Does this answer your question? [What is the difference between Promises and Observables?](https://stackoverflow.com/questions/37364973/what-is-the-difference-between-promises-and-observables) – Beller Feb 23 '21 at 06:29
  • I'm not sure what you mean by angular is waiting. Angular triggers the post request and then listens to the subscription. Do you have a loading panel that you see while the post is completed? What does waiting mean? – Alvin Saldanha Feb 23 '21 at 06:31
  • @Alvin Until the process is completed on the server side, html that is not related to http communication is displayed. After the post is complete, other GET requests will reach next(in subscribe). – Zalve Feb 23 '21 at 06:59
  • Are any of the requests piped? Do you use switchMap or any other RxJS operators? – Alvin Saldanha Feb 24 '21 at 00:11
  • @Alvin I'm using 'tap' and 'of' in GET request. (e.g.) return this.http.get(url, httpOptions) .pipe( tap(contents => this.log('fetched contents')), catchError(this.handleError('getDummy', [])) ); – Zalve Feb 24 '21 at 02:14
  • What I meant to ask was if the get request was nested within the post. But doesn't seem like it. It is very hard without seeing the code where get is called to diagnose the issue. But your post request looks fine. In your example you call the post on ngOnInit. Do you call the get also ngOnInit or is it on a different event? Would it be possible to create a sample stackblitz to recreate your issue? – Alvin Saldanha Feb 24 '21 at 06:55
  • @Alvin I'm calling both post and get requests on ngOnInit. It may take some time, but I would like to make a sample if possible. – Zalve Feb 24 '21 at 22:18

0 Answers0