Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an observable with a delay

Question

For testing purposes, I'm creating Observable objects that replace the observable that would be returned by an actual http call with Http.

My observable is created with the following code:

fakeObservable = Observable.create(obs => {   obs.next([1, 2, 3]);   obs.complete(); }); 

The thing is, this observable emits immediatly. Is there a way to add a custom delay to its emission?


Track

I tried this:

fakeObservable = Observable.create(obs => {   setTimeout(() => {     obs.next([1, 2, 3]);     obs.complete();   }, 100); }); 

But it doesn't seem to work.

like image 340
Adrien Brunelat Avatar asked Feb 23 '17 11:02

Adrien Brunelat


People also ask

How do I add a delay to observable?

Subscribe to a source observable. When a new value arrives from a source observable, add the value to internal cache and start a delay interval for that value. Once the interval elapses, send the corresponding value to the observer. Once the source observable completes, send the complete notification to the observer.

How do you delay in RxJS?

In RxJS you can set the per-item delay in two ways: by passing a number of milliseconds into the delay operator (which will delay each emission by that amount of time), or by passing in a Date object (which will delay the beginning of the sequence of emissions until that absolute point in time).

How do I create a custom observable?

Steps to create custom observable in Angular First thing is to create an instance of Observable for which you can use Observable. create() method. This created observable instance defines a subscriber function. The subscriber function defines how to obtain or generate values to be published.


2 Answers

Using the following imports:

import {Observable} from 'rxjs/Observable'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/delay'; 

Try this:

let fakeResponse = [1,2,3]; let delayedObservable = Observable.of(fakeResponse).delay(5000); delayedObservable.subscribe(data => console.log(data)); 

UPDATE: RXJS 6

The above solution doesn't really work anymore in newer versions of RXJS (and of angular for example).

So the scenario is that I have an array of items to check with an API with. The API only accepts a single item, and I do not want to kill the API by sending all requests at once. So I need a timed release of items on the Observable stream with a small delay in between.

Use the following imports:

import { from, of } from 'rxjs'; import { delay } from 'rxjs/internal/operators'; import { concatMap } from 'rxjs/internal/operators'; 

Then use the following code:

const myArray = [1,2,3,4];  from(myArray).pipe(         concatMap( item => of(item).pipe ( delay( 1000 ) ))     ).subscribe ( timedItem => {         console.log(timedItem)     }); 

It basically creates a new 'delayed' Observable for every item in your array. There are probably many other ways of doing it, but this worked fine for me, and complies with the 'new' RXJS format.

like image 166
MikeOne Avatar answered Oct 18 '22 04:10

MikeOne


In RxJS 5+ you can do it like this

import { Observable } from "rxjs/Observable"; import { of } from "rxjs/observable/of"; import { delay } from "rxjs/operators";  fakeObservable = of('dummy').pipe(delay(5000)); 

In RxJS 6+

import { of } from "rxjs"; import { delay } from "rxjs/operators";  fakeObservable = of('dummy').pipe(delay(5000)); 

If you want to delay each emitted value try

from([1, 2, 3]).pipe(concatMap(item => of(item).pipe(delay(1000)))); 
like image 33
Adrian Ber Avatar answered Oct 18 '22 04:10

Adrian Ber