Skip to content

Commit

Permalink
feat(task-description-image-upload): implement retryFc to retry the…
Browse files Browse the repository at this point in the history
… api in case of upload failure after 1 sec
  • Loading branch information
Sujit committed Sep 30, 2024
1 parent 4b6c7b8 commit ab22820
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/frontend/src/utils/callApiSimultaneously.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import axios from 'axios';
import { toast } from 'react-toastify';

// function that calls the api simultaneously
export default async function callApiSimultaneously(urls: any, data: any) {
const promises = urls.map((url: any, index: any) =>
axios.put(url, data[index]),
// eslint-disable-next-line no-promise-executor-return
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

const retryFc = async (url: string, singleData: any, n: number) => {
try {
return await axios.put(url, singleData);
} catch (err) {
if (n === 1) throw err;
delay(1000); // 1 sec delay
// eslint-disable-next-line no-return-await
return await retryFc(url, singleData, n - 1);
}
};

const promises = urls.map(
(url: any, index: any) => retryFc(url, data[index], 3), // 3 entries for each api call
);
const responses = await Promise.all(promises);
return responses;

try {
const responses = await Promise.all(promises);
return responses;
} catch (err) {
toast.error('Error occurred on image upload');
throw err;
}
}

0 comments on commit ab22820

Please sign in to comment.