Use Bing to convert API endpoint call-out from Sequential to Parallel
Tags: javascript, beginners, programming, python
Published: 2023-03-27 16:27:37

## Requirement
Retrieve content from 20 google sheets within single Google spreadsheet.

## How should we approach this?
- Retrieve list of all sheets as array
- Iterate over the array and retrieve content for each sheet

I asked Bing to get me python code to do so.

> ### Prompt - code to make request
`Write python code to get content of range a:a from all available sheets of google spreadsheet`
### Bing
![bing response with code to get sheet data of range from all sheets within single spreadsheet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x7mgm5176hpykmb9y17.png)

This is okay, but what if you have 100 sheets within single spreadsheet. 

Each request would be around 0.7 seconds, waiting time will be 100*0.7 = 700 seconds. around 11 minutes.

Follow up question to improve performance.

> ### Prompt - improve performance
`I am trying to retrieve result from 100 sheet_names array. is there a way you can improve performance of this code? Without giving any directions.`
### Bing
![bing response with code to get sheet data of range from all sheets within single spreadsheet with improved performance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/az7kmduplmqpg1kglup6.png)

Looks good, it suggested me to use `batch_get` function of googleapis.


However, what if endpoint or API you are trying to call is simply an http request to an api or endpoint?

## Simplified Problem
Let's simplify problem statement
Retrieve users details for each users with id from 1 to 100

> ### Prompt
`generate python code to make HTTP request to API https://64217eb134d6cd4ebd74a672.mockapi.io/api/v1/users/{id} for, id from 1 to 30`
### Bing
![bing response with code to make http request to an endpoint or API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mymh4yfi3q4a4a7npnjw.png)

**Execution time:** 13 seconds

> ### Prompt
`I am trying to retrieve information for 100 users, it is slow. can you improve performance?`
### Bing
![bing response with code to make http request to an endpoint or API with improved performance with parallel execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvorlnaskrt0f5c72bq8.png)
**Execution time:** 1 seconds

Pretty cool 🤖, isn't it? Though now you need to know following things.
- Handle 429, rate limiting error
- The impact of multithreaded python script

**FYI** 😉

![bing response to explain impact of multithreaded python script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3myfefwv5ed1777f5y9d.png)




## Typescript
Node is by default single threaded application and works on call back, since introduction of async-await, sometimes I see beginners use it to make their code synchronous. 

> ### Prompt
`generate typescript code to make HTTP request using fetch to API https://64217eb134d6cd4ebd74a672.mockapi.io/api/v1/users/{id} for, id from 1 to 30`
### Bing
![bing response with typescript code to make http request to an endpoint or API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsqt62q6dtszlqpvdzmb.png)

**Execution time:** 8.5 seconds

> ### Prompt
`It is slow, can you make all fetch in parallel?`
### Bing
![bing response with typescript code to make http request to an endpoint or API with improved performance using promise.all](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2quw5vnhi8japddrkq7d.png)

**Execution time:** 0.6 seconds



# Moral of the story
Start utilizing this AI based tools to enhance your productivity 😊✌️



**Fun fact:** cover image is created by bing.com/create