How to Return multiple functions and values while working with REST APIs (Part 1)
Tags: javascript, node, express, api
Published: 2021-01-13 12:48:02

As a developer, you interact with REST APIs directly or indirectly, every day at work. This makes it important now more than ever to keep learning and keep improving your REST development skills. 

In this part 1 of the series, I will show you how to use the `q library` function `.spread()`  to specify multiple functions to use as the callbacks and explore the possibilities with this library, while in part 2, we'll not be using the  `q library`.  `q library` is used for creating custom promises and can be quite handy when working with promises.

We'll be exploring how to execute multiple functions at the same time and use `.spread()` to grab the response from each of the functions. `.spread()` is a `q library` function which can be use as a replacement for `.then()` .



> If you have a promise for an array, you can use .`spread()` as a replacement for `.then()` The `spread` function “spreads” the values over the arguments of the fulfillment handler. The rejection handler will get called at the first sign of failure. That is, whichever of
> the received promises fails first gets handled by the rejection handler. (Source:https://documentup.com/kriskowal/q/)


**Here's the concept of returning multiple values and using `.spread()`**

```javascript
 return [a,b,c];
        .spread(a,b,c)=>{
        //
        }
```
    

This comes in handy when building APIs that query multiple tables and/or calling multiple endpoints.


> For the sake of this tutorial and to keep it simple, I'll be making use of `chuck-norris` API https://api.chucknorris.io/ 
> This is a free JSON API for hand-curated Chuck Norris facts.


**The modules used:**

- **Axios :** This module makes HTTP request seamless. Check it out [here](https://www.npmjs.com/package/axios)
- **q :** This module is used for creating custom promises. Check it out [here](https://www.npmjs.com/package/q)
- **Express:** Fast, unopinionated, minimalist web framework for [node](http://nodejs.org/). Check it out [here] (https://www.npmjs.com/package/express)


**The endpoints:**
In this tutorial, we'll be making requests to two chuck-norris APIs

- **Jokes:** https://api.chucknorris.io/jokes/random
- **Categories:** https://api.chucknorris.io/jokes/categories

To install all the  dependencies required for this task, run  `npm install q axios express`

**Let's get our hands dirty** 🔥

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


`Line 8` : `q.fcall()` is `q library` function and it is used to create a promise.

`Line 13`: `return [joke, categories]` stops the execution of the block of code and returns the HTTP request made to `chuck-norris` API in `line 9 and 10`.


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


`Line 15`: `.spread()` works like `.then()` but takes in multiple arguments unlike `.then()` that takes up to two arguments: callback functions for the success and failure cases of the Promise. 

`line 17 -18`: I decide to filter the API response to what I needed. You can filter the response as you please also.

`Line 22-27` : I introduced `if` statement logic to check if the returned response from the API contains data. This is purely me ensuring that I checked if `data` field is returned in the response before proceeding.


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


`Line 31` : The reason why I decided to randomize 0-15 is because the categories API returns close to 15-16 categories info which I really don't want to expose all, I preferred to randomly pick one as my response which is what I did in `Line 39`. Again, this just me doing my thing 😃😃 not really necessary. You can do as you please 😉


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


`Line 34-42`: I decided to create and structure my response. 😉 

`Line 43`: Finally send back my response

`Line 45-47` : I ensured I catch any error

One thing I didn't mention which might be confusing for a beginner is `line 6`
I choose the endpoint name as `/chuck-norris` based on my mood right now 😁 you can choose any name you want.


**Now let's run our APP** 🚀🚀🚀

On `Line 49-52` I have setup 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 this endpoint: 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!

With `q library` functions , we (me and you 😉) were able to create a promise with `q.fcall()` where I made two API request to chuck norris APIs and return the response of the two calls in an array and finally used `.spread()` which is also a  `q library` function that takes in multiple arguments to use as the callbacks.

Happy coding 🍻

See Project [here](https://glitch.com/edit/#!/chuck-norris-api)

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