ChatGPT refactor/rewrite code using Promise.All (send requests in parallel)

Original link: https://justyy.com/archives/62431

In one of my projects, I have the following code (Node/ Javascript ), which (sequentially) calls some APIs. These HTTPS requests can actually be done in parallel because they are independent of each other. The results are then aggregated into an array.

I know that Promise.all can be used to parallelize these requests, so I asked ChatGPT how to do it to see if it can understand and give the correct code:

I first ask ChatGPT-3.5 in English: Rewrite the following Python code using the Promise.all to parallelize the requests:

 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); 
  
    } 
  
  }

ChatGPT-3.5 did understand the code and what I meant, gave the correct result, and ChatGPT also gave the code explanation.

 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 of promises and push the promise of each callAPI into it. Then, we use Promise.all to execute all promises at the same time. The results are stored in the results array and then iterated using forEach. Inside the forEach loop, we can access the result of each API call via succ and the corresponding plugin name via pluginNames[index]. Then, push the result into the appropriate array of result objects.

chatgpt-3.5-refactor-code-promise-all ChatGPT 使用 Promise.All 重构/重写代码(并行发送请求) ChatGPT (OpenAI) 人工智能 (AI) 代码审核 程序设计 软件工程

ChatGPT-3.5 Refactors code using Promise.all

Later I asked again in Chinese, but this time I chose ChatGPT-4 but the code given was not as good as the one given above.

Below is the code for a parallelized version based on Promise.all. In this version, all plugins will call API at the same time, and then put the result into the enabled_plugins or disabled_plugins list.

 let result = { 
  
    "node": node, 
  
    "time": new Date().toISOString(), 
  
    "enabled_plugins": [], 
  
    "disabled_plugins": [], 
  
}; 
  
 
  
const pluginNames = Object. keys(plugins); 
  
const promises = pluginNames. map(plugin => { 
  
    const api = plugins[plugin]; 
  
    const params = api. slice(1)[0]; 
  
    return callAPI(node, plugin, api[0], params) 
  
        .then(succ => { 
  
            if (succ) { 
  
                result["enabled_plugins"].push(plugin); 
  
            } else { 
  
                result["disabled_plugins"].push(plugin); 
  
            } 
  
        }); 
  
}); 
  
 
  
await Promise. all(promises);

Note: This version of the code may be faster, but it will make the order of the plugins in enabled_plugins and disabled_plugins no longer follow the order in the original plugins. You may need to reorder enabled_plugins and disabled_plugins after Promise.all if order matters.

Also, since all API calls in this version of the code are parallelized, if you have a large number of plugins, you may be putting a lot of load on your system or network at the same point in time. If this is a problem, you may want to consider using concurrency control methods to limit the number of simultaneous API calls.

chatgpt-4-promise-all-refactor ChatGPT 使用 Promise.All 重构/重写代码(并行发送请求) ChatGPT (OpenAI) 人工智能 (AI) 代码审核 程序设计 软件工程

The code given by ChatGPT-4 is not as good as that given by ChatGPT-3.5

The code can also be used, but as ChatGPT-4 said, the final array is random and does not meet my requirements.

I can write it myself, but in the past I might search for the usage and examples of Promise.all, and then write and test it. I can easily get what I want through ChatGPT. In most cases, I can use it directly, saving After going through the thinking process, the most is to adjust the code and change the BUG. Through ChatGPT, the work productivity has been greatly improved. In this example, it can save me a few minutes of time.

I believe that Microsoft’s Github Copilot can achieve similar amazing results.

Why does ChatGPT not give exactly the same results every time?

You may be curious: why sometimes the answers given by ChatGPT are different.

  • One is the version, the data set size of ChatGPT-3.5 and ChatGPT-4 training is different. Naturally the results are unlikely to be exactly the same.
  • One is that the results obtained in different languages ​​are different. The training data based on Chinese and English AI is different, and there may be Bias bias.
  • Another thing is that the questioning method Prompt Engineering will also lead to different results. To get good results, you must ask the right questions.

Even if it is the same question, it is unlikely that the regenerated answer will be exactly the same, because ChatGPT generates an answer word by word. Every time a word is generated, it will predict the probability of the next word, and then form a new Context to predict the following Words, the probability of each word is different, there is a certain degree of randomness.

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

There are a total of 938 Chinese characters in this article, count them right.

ChatGPT uses Promise.All to refactor/rewrite the code (send requests in parallel) . ( AMP mobile acceleration version )

Scan the QR code and share this article to WeChat Moments

75a5a60b9cac61e5c8c71a96e17f2d9c ChatGPT 使用 Promise.All 重构/重写代码(并行发送请求) ChatGPT (OpenAI) 人工智能 (AI) 代码审核 程序设计 软件工程

The post ChatGPT uses Promise.All to refactor/rewrite the code (send requests in parallel) first appeared on Xiao Laizi’s British life and information .

This article is transferred from: https://justyy.com/archives/62431
This site is only for collection, and the copyright belongs to the original author.