In one of my project, I have the following code (Node/Javascript) that invokes a few APIs one by one (sequentially). However, these HTTPS requests can be done in parallel as they are independent of each other. I just need to aggregate the results in an array.
I asked ChatGPT to rewrite using promise all to call the API in parallel:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | let result = { "node": node, "time": new Date().toISOString(), "enabled_plugins": [], "disabled_plugins": [], }; const pluginNames = Object.keys(plugins); for (let i = 0; i < pluginNames.length; ++ i) { const plugin = pluginNames[i]; const api = plugins[plugin]; const params = api.slice(1)[0]; const succ = await callAPI(node, plugin, api[0], params); if (succ) { result["enabled_plugins"].push(plugin); } else { result["disabled_plugins"].push(plugin); } } |
let result = { "node": node, "time": new Date().toISOString(), "enabled_plugins": [], "disabled_plugins": [], }; const pluginNames = Object.keys(plugins); for (let i = 0; i < pluginNames.length; ++ i) { const plugin = pluginNames[i]; const api = plugins[plugin]; const params = api.slice(1)[0]; const succ = await callAPI(node, plugin, api[0], params); if (succ) { result["enabled_plugins"].push(plugin); } else { result["disabled_plugins"].push(plugin); } }
And ChatGPT (3.5, not to mention ChatGPT-4) does understand the code and propose the correct Refactoring or code optimisation that turns the sequental calls into Promise.all which calls the requests in parallel and aggregates the results. ChatGPT also gives the explanation.
To call the API in parallel using Promise.all, you can refactor the code as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | let result = { "node": node, "time": new Date().toISOString(), "enabled_plugins": [], "disabled_plugins": [], }; const pluginNames = Object.keys(plugins); const promises = []; for (let i = 0; i < pluginNames.length; ++i) { const plugin = pluginNames[i]; const api = plugins[plugin]; const params = api.slice(1)[0]; promises.push(callAPI(node, plugin, api[0], params)); } const results = await Promise.all(promises); results.forEach((succ, index) => { const plugin = pluginNames[index]; if (succ) { result["enabled_plugins"].push(plugin); } else { result["disabled_plugins"].push(plugin); } }); |
let result = { "node": node, "time": new Date().toISOString(), "enabled_plugins": [], "disabled_plugins": [], }; const pluginNames = Object.keys(plugins); const promises = []; for (let i = 0; i < pluginNames.length; ++i) { const plugin = pluginNames[i]; const api = plugins[plugin]; const params = api.slice(1)[0]; promises.push(callAPI(node, plugin, api[0], params)); } const results = await Promise.all(promises); results.forEach((succ, index) => { const plugin = pluginNames[index]; if (succ) { result["enabled_plugins"].push(plugin); } else { result["disabled_plugins"].push(plugin); } });
In the refactored code, we create an array promises and push each callAPI promise into it. Then, we use Promise.all to execute all the promises concurrently. The results are stored in the results array, which is then iterated over using forEach. Inside the forEach loop, we can access the result of each API call using succ and the corresponding plugin name using pluginNames[index]. The result is then pushed to the appropriate array in the result object.
Prior to ChatGPT – I would just google the usage of Promise.all and then rewrite it myself. With ChatGPT, It saved me a couple of minutes – my productivity is highly increased!
I believe similarly, I can achieve this using the Microsoft Github Copilot.
ChatGPT Gives Different Results
You might wonder that why ChatGPT gives slightly different results every runs, even on the same Prompt?
- One is the version, the dataset sizes used to train ChatGPT-3.5 and ChatGPT-4 are different. Naturally, the results are unlikely to be exactly the same.
- Another is that the results obtained from different languages are also different. The training data used for the Chinese and English AIs is different, which might also lead to biases.
- The last one is that the way of asking the question, known as Prompt Engineering, can also lead to different results. In order to get good results, the question must be accurately framed.
Even with the same prompt, regenerating answers is unlikely to be exactly the same, because ChatGPT generates answers word by word. Each time it generates a word, it predicts the probability of the next word, then forms a new context to predict the following words. The probability of each word is different and there is a certain degree of randomness.
ChatGPT Use Cases
- How ChatGPT Solves a Math Puzzle?
- ChatGPT Designs a Health Check Tool in Node.Js to Run on Free VPS Instance
- ChatGPT-4 uses Math Wolfram Plugin to Solve a Math Brain Teaser Question
- ChatGPT Refactors/Rewrites the Code using Promise.All - Sending Requests in Parallel
- ChatGPT Use Case: A Good Personal AI Assistant to Help You Write Emails
- Programming Language Conversion Tool based on ChatGPT AI
- ChatGPT4: integrated with DALL.E: Generate a Merry Christmas theme girl in Red Dress
- Have you started using ChatGPT-4o?
ChatGPT - AGI (Artificial General Intelligence)
- Blog Traffic Declines after ChatGPT is Widely Adopted
- What is going to be the next trend in technology in the next 10 years?
- Asking ChatGPT the Price of a Bitcoin (Does ChatGPT Predict or Estimate?)
- On Waiting List of ChatGPT 4 API
- The System Design of Steem Blockchain (ChatGPT DApps) Bots
- Integrating ChatGPT Prompt AI to STEEM blockchain
- ChatGPT Gives a Correct Leetcode Solution (Rust: Binary Search Algorithm) after Several Attempts
- ChatGPT Fails to Solve the Sudoku
- OpenAI Text Classifier Fails to Detect ChatGPT Text
- Quantum Computing in Simple Terms (ChatGPT vs Notion AI)
- How ChatGPT Impacts UGC (User Generate Content)?
- The Young Prompt Engineers
ChatGPT Fails to
–EOF (The Ultimate Computing & Technology Blog) —
a WordPress rating system
Last Post: Teaching Kids Programming - Algorithms to Compute the Minimum String Length After Removing Substrings (Brute Force + Stack)
Next Post: How to Earn Microsoft Claim Points and Redeem Rewards by using Bing Search Engine and Edge Browser?