Simulating Request Delay With RxJS

Ole Ersoy
Jan 5, 2019

--

Photo by insung yoon on Unsplash

Scenario

We are requesting post data of type Post and we wish to simulate the retrieval of a single Post instance.

Approach

Lets first create the Post type:

type Post = { title: string, content: string };

Next use the RxJS delay operator to simulate the delay in retrieving the Post instance like this:

const post:Observable<Post> = of( 
{title: 'Simulating HTTP Requests',
content: 'This is off the hook!!'})
.pipe(delay(2000));
post.subscribe(console.log);

The Post instance is logged after the 2 second delay introduced by delay(2000) .

Demo

--

--

Ole Ersoy
Ole Ersoy

Written by Ole Ersoy

Founder of Firefly Semantics Corporation

Responses (2)