Observable from Fetch API

Saurabh
Saurabh
Apr 9, 2019 · 1 min read

Being a JS developer, No matter what JS library/Framework you work with, There is always a need to understand how things work under the hood.

I recently started working with Angular, And of course, I had to use it’s HTTP service for my project, And there is one thing that caught my eyes that HTTP call returns an (RxJs)Observable (I was expecting Promise :)), Which makes life a lot easier.

Which raises a question, how angular does that, Or how can I do that without using Angular’s HttpClient, So I decided to write my own service to do that.

I chose to write this service for Fetch API (Although Angular’s HTTPClient rests on the XMLHttpRequest interface exposed by browsers) as It provides an API for both client-side applications and server-side which in returns facilitates Server-side rendering.

Enough theory, Let’s code now

Basic understanding of RxJS Observable and Fetch API is must to understand this code

Now you can call this method to serve the purpose as follows

Saurabh

Written by

Saurabh

JavaScript Developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade