ChatGPT Refactors/Rewrites the Code using Promise.All – Sending Requests in Parallel


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-3.5-refactor-code-promise-all ChatGPT Refactors/Rewrites the Code using Promise.All - Sending Requests in Parallel Artificial Intelligence ChatGPT (OpenAI) javascript nodejs

ChatGPT-3.5 Refactors code using Promise.all

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

ChatGPT - AGI (Artificial General Intelligence)

ChatGPT Fails to

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
822 words
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?

The Permanent URL is: ChatGPT Refactors/Rewrites the Code using Promise.All – Sending Requests in Parallel

Leave a Reply