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)
.