In this part 2, we'll be using functions native to Javascript except of course Axios
library to make API calls.
Here's the concept of returning multiple values
const multipleAPI = () => {
const joke = await axios.get('https://api.chucknorris.io/jokes/random');
const categories = await axios.get('https://api.chucknorris.io/jokes/categories');
// This returns the response from joke and categories
return [joke, categories]
}
//Call the function
multipleAPI();
To return multiple values, pass it in an array return [joke, categories]
In this tutorial, we'll be achieving the same result as we did in part 1
but this time without q library
functions like .spread()
, q.fcall
.
We're purely using functions native to Javascript just in case a library is deprecated or a version is deprecated, we won't be stuck π
Two functions fetchJokes
and callJoke
were created. These are just variable names, you can use whatever name you really want.
In the function declaration of fetchJokes
all API calls were made and the responses returned accordingly i.e return [joke, categories]
in Line 10
.
ThefetchJokes
function is then invoked in the callJoke
function, It's response is destructured in Line 17
. You can checkout more on destructing here
Since the categories API returns up to 15 categories, In order to minimize the number of data exposed, I decided to generate a random number between 0-15, as shown on Line 19
. The random number generated was then passed as an index to the array response returned from the categories API as shown on Line 26
. Please note that this is not compulsory, you can achieve this however you see fit π
Line 31
is very important !!!.
This is where callJoke()
is called or triggered. If this was not called, the block code Line 16-29
won't run. Also, note callJoke()
was called outside the block of code Line 16-29
Now let's run our APP πππ
On Line 33
I have set up my app to run on port 3000
.
I'm using nodemon on my local machine, so I'm running this command; nodemon app.js
[You can install nodemon
as a dev dependency npm install --save-dev
]
Open your Postman or Insomnia or even your browser since it's a GET request
Make a request to : http://localhost:3000/chuck-norris
Voila π
Everything is working!
We've been able to return multiple functions and values while working with REST APIs both with q library
in part 1 and native Javascript functions in Part 2.
EXTRAS:
As a developer, you should always have validation in mind and be 100 steps ahead π. Imagine if no data is returned when fetchJokes()
is called on Line 20
?. This will cause a code break in Line 25 -26
. To mitigate this, just after line 20
, before declaring the response variable on Line 21
, we can do a check to be sure that the API returned a data response before proceeding, if not throw an error response.
if(!joke.data.value){
res.json({status:"failed", message:"joke not available"})
throw new Error('ERROR_RESPONSE_SENT');
}
if(!categories.data){
res.json({status:"failed", message:"categories not available"})
throw new Error('ERROR_RESPONSE_SENT');
}
Observe that after returning the error response, I ensured that I explicitly break out of the scope throw new Error('ERROR_RESPONSE_SENT');
The reason is so that the other lines of code do not continue to run after I have returned the response. A Javascript developer MUST keep this in mind. EXPLICITLY break out of scope.
Happy coding π»
See Project here
Code review credit : Jimi Ayomide
Top comments (0)