Now we’re going to piece together a POST request using
Feel free to refer to the
await diagram below at any point while completing this exercise:
At the top of main.js create an
async arrow function and save it to a
async keyword will ensure that the function returns a promise.
In the code block for
getData, we should add a
try statement with an empty code block.
In case things go wrong, we need some code to handle that. Below the
try code block, add a
catch statement and pass in
error as an argument.
Then, in the
catch statement code block, log
error to the console.
We now have to consider what we want to do inside of the
try code block. Since we are making a POST request, we should start by using the
await keyword before calling the
We will have to save the returned promise in a variable called
response using the
fetch() call that we just made, pass in the following URL to the function as a string for the first argument:
Then as our second argument, let’s pass in an empty object for now.
Let’s now fill in the request options in our second argument.
First, add the
method property and the value is
Then we have to include a
body property and the value is
Remember to separate the properties with a comma.
After the code block of
response, we should create an
if statement that checks the
ok property of the
Inside the code block of the conditional statement,
await the resolution of calling the
.json() method on
response. Save the returned object to a variable called
jsonResponse using the keyword
Now that we have what we want, we should return
jsonResponse directly below the code written in the previous step.
Like with previous boilerplate exercises, we’re practicing writing code. Normally, we would want to do more beyond this step of returning
new Error() with the message
Nice work, didn’t that feel very similar to making a GET request?