3

I'm executing some requests in loop for the same API. On server-side I need to check the previous result to make some changes. But I can't see the previous result if requests are more than one in loop.

in component:

this.service.createEmail(...).delay(5000).debounceTime(5000).timeout(5000).subscribe(...);

and in service:

createEmail(...): Observable<Json> {
    return this.http
            .post('url',
              JSON.stringify(model),
              { headers: this.headers })
            .delay(5000).debounceTime(5000).timeout(5000)
            .map((response: Response) => {
                return this.responseService.extractData(response);
            })
            .catch(error => {
                return this.errorService.handleError(error);
            });
        }

didn't help me. Needed imports exist. Just js setInterval helped me.

How can I do delay before http request in other ways?

3
  • have you tries setTimeout(() => { .. }, 5000) ? Maybe that might help Commented Dec 1, 2017 at 10:50
  • Yes, I tried, it didn't help me also. Commented Dec 1, 2017 at 10:58
  • 1
    The problem is that you are making the requests in a loop. The loop doesn't wait for timeout and tries to fire all requests in nearly the same time. And debounce will make sure that only the last request is fired. Thats why you can't see more than 1 response. If you can drop the loop completely and use observable chaining with concatMap that will work. Commented Dec 1, 2017 at 12:19

1 Answer 1

1

You can use switchMap and timer:

createEmail(...): Observable<Json> {
   return Observable
            .timer(5000)
            .switchMap(() => 
                this.http.post(
                  'url', 
                  JSON.stringify(model),
                  {headers: this.headers}
                )
            )
            .map((response: Response) => {
                return this.responseService.extractData(response);
            })
            .catch(error => {
               return this.errorService.handleError(error);
            });
}

Before the switchMap you can add debounceTime and another rx operator.

I invite you to check the documentation of switchMap to have more information: https://www.learnrxjs.io/operators/transformation/switchmap.html

You can use .delay when you don't want delivered the result of the observable immediately. For example, I use it when I want simulate the delay of the response of the API.

Sign up to request clarification or add additional context in comments.

6 Comments

No, simply requests doesn't go to the server
@A.Gladkiy Do you import specifically each rxjs operator? Like import "rxjs/add/operator/map"
Yes, switchMap, map, timer, catch
@A.Gladkiy Observable too? And you have wait 5 sec :)
Yes, Observable too, I think I would have errors in that case
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.