'Nodejs: axios request parallel way
I have a code which execute request sequentially
try {
const results = [];
for await (let tc of testCases) {
const { data } = await axios.post("URL",
{
command_line_arguments: "",
compiler_options: "",
redirect_stderr_to_stdout: true,
source_code: source_code,
language_id,
stdin: tc.input,
expected_output: tc.output,
}
);
results.push({ text: tc.text, input: tc.input, output: tc.output, testType: tc.testType, ...data });
}
it works, but it is very slow.
I am looking to request all of them in parallel way.
Note: I have tries Promise all,Promise.allsettled, and Axios.all. Some how it didnt worked.
My solution:
const runTestCases = async (testCases: ITestCase[], source_code, language_id) => {
try {
const requests = createRequests(testCases, source_code, language_id);
const result = await Promise.all(requests.map(async (response) => {
const { data }:any = await response;
return data;
}));
return result;
} catch (error) {
throw new BadRequestError(error?.message);
}
};
/**
* Create array of request to run test cases in parallel
* @param testCases
* @param advance
* @returns
*/
const createRequests = (testCases: ITestCase[], source_code: string, language_id: string) => {
const requests = testCases.map((tc) => {
return () => axios.post(process.env.JUDGE0_HOST,{
redirect_stderr_to_stdout: true,
source_code: source_code,
language_id,stdin: tc.input,
expected_output: tc.output
})})
return requests;
};
output:
[ undefined, undefined ]
Am I doing wrong ?
Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
