How to Return multiple functions and values while working with REST APIs (Part 2)
Tags: javascript, express, node, api
Published: 2021-02-10 13:20:38

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

```javascript
 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](https://dev.to/olufemi/how-to-return-multiple-functions-and-values-while-working-with-rest-apis-part-1-2cm7)
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 😉 


   ![](https://paper-attachments.dropbox.com/s_2A85FCEB16F65ED0E222DA87A0DE2CB291B5ABE1CD3AD8F43080391C5383CE7B_1609929483089_Screenshot+2021-01-06+at+11.35.58.png)

![](https://paper-attachments.dropbox.com/s_2A85FCEB16F65ED0E222DA87A0DE2CB291B5ABE1CD3AD8F43080391C5383CE7B_1609929512566_Screenshot+2021-01-06+at+11.37.27.png)


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

The`fetchJokes` function is then invoked in the `callJoke` function, It's response is destructured in `Line 17`.  You can checkout more on destructing [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)


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`_]

![](https://paper-attachments.dropbox.com/s_D773CEEB668B1FF685C00AF1AD02488D4C7662A9D6AF3833BF43B9FF1D299AF7_1591748665365_Screenshot+2020-06-10+01.24.05.png)

Open your Postman or Insomnia or even your browser since it's a GET request 

Make a request to : http://localhost:3000/chuck-norris
![](https://paper-attachments.dropbox.com/s_D773CEEB668B1FF685C00AF1AD02488D4C7662A9D6AF3833BF43B9FF1D299AF7_1591748910421_Screenshot+2020-06-10+01.26.56.png)


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](https://dev.to/olufemi/how-to-return-multiple-functions-and-values-while-working-with-rest-apis-part-1-2cm7) 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.

```javascript
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](https://glitch.com/edit/#!/chuck-norris-2)

Code review credit : [Jimi Ayomide](https://www.linkedin.com/in/jimioniayomide/)


Do you like this article? Hit me up on [Twitter](https://twitter.com/femioba_) or [linkedin](https://www.linkedin.com/in/olufemi-obafunmiso/)