This assumes Typescript, and using the got
library:
import got from "got";
And now on how to handle the request gracefully:
const getCompletion = async (url: string): Promise<ReturnedDataType> => {
const response = (await got
.get(url, {
retry: {
limit: 10,
methods: ["GET"],
statusCodes: [429],
calculateDelay: ({ computedValue }) => {
const delay =
computedValue + Math.random() * 1000 || 100 + Math.random() * 10000;
console.log(`Retrying fetching completion for ${url} in ${delay}, ms`);
return delay;
},
},
})
.json()) as ReturnedDataType;
console.log(response);
return response;
};