Web Fetch API
Full disclosure, I normally use
axios as my library of choice for http fetching. This is in case I get asked this question in the upcoming interviews and they want me to use a native web API.
We are going to use Nodejs for this, so just create a file
index.js anywhere you want.
fetch API is promise based, so let's take a look as a basic implementation and then using
Because we are using node, we need to download the
npm package that uses the same API. Let's setup the repo and package with the following:
We will basically run the script as blocking, but syntax-wise we will run through both an
await version and standard.
We will use
https://postman-echo.com/post as our endpoint to essentially test if we make a successful call and look for the 200 status.
If we run
node index.js from our CLI we will see
Request succeeded with JSON response 200.
With this method, we are returning a
Promise to begin with but manually running our
try/catch block with a
resolve/reject resolution based on failure or success.
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.