Home > Net >  Angular RxJS How to make delay with time came from previous pipe
Angular RxJS How to make delay with time came from previous pipe

Time:10-21

getMessageInSomeTime(): Observable<string> {
    const someAsyncData = {
      text: 'Some text',
      dealyTime: 5000
    };
    return of(someAsyncData).pipe(
      map(response => {
        delay(response.dealyTime); // Here  I have to make pause before return
        return response.text;
      })
    );
  }

Now this code has syntax error on delay() pipe call. How to make the code properly?

CodePudding user response:

I believe you're looking for something like this:

return of(someAsyncData).pipe(
  switchMap(response =>
    delay(response.dealyTime).pipe(
      mapTo(response.text)
    )
  )
);

You want to switch from your observable to a delayed observable which maps to your result.

  • Related