Simulating Request Delay With RxJS

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store