Add random timeout up to 500ms

This commit is contained in:
Senad Uka
2020-09-10 18:08:24 +02:00
parent b27d9d3499
commit 0c2f8d11ee

View File

@@ -6,6 +6,10 @@ const {
SCRAPER_API_BASE_URL
} = require("../config/appConfig");
const timeout = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
const fetch = async (url, options = {}) => {
const newOptions = Object.assign({}, options);
if (!newOptions["headers"]) {
@@ -21,7 +25,8 @@ const fetch = async (url, options = {}) => {
const urlAdaptedForScraping = USE_SCRAPER_API
? `${SCRAPER_API_BASE_URL}?api_key=${SCRAPER_API_KEY}&url=${urlToFetchThroughAPI}`
: url;
const randomPauseMS = Math.floor(Math.random() * Math.floor(500));
await timeout(randomPauseMS);
return nodeFetch(urlAdaptedForScraping, newOptions);
};