From b30b0f45a6ee12dd5e5eb207ba906c5bc1ef32e2 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Thu, 10 Sep 2020 18:23:01 +0200 Subject: [PATCH] trying different approach --- app/helpers/fetchWrapper.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/helpers/fetchWrapper.js b/app/helpers/fetchWrapper.js index d03addb..a6fd4b4 100644 --- a/app/helpers/fetchWrapper.js +++ b/app/helpers/fetchWrapper.js @@ -6,9 +6,14 @@ const { SCRAPER_API_BASE_URL } = require("../config/appConfig"); -const timeout = (ms) => { +const async timeout = (method, ms) => { console.error("Waiting for ", ms); - return new Promise(resolve => setTimeout(resolve, ms)); + return new Promise(resolve => { + setTimeout(() => { + await method(); + resolve(); + }, ms); + }); } const fetch = async (url, options = {}) => { @@ -27,8 +32,7 @@ const fetch = async (url, options = {}) => { ? `${SCRAPER_API_BASE_URL}?api_key=${SCRAPER_API_KEY}&url=${urlToFetchThroughAPI}` : url; const randomPauseMS = Math.floor(Math.random() * Math.floor(1500)) + 500; - await timeout(randomPauseMS); - return nodeFetch(urlAdaptedForScraping, newOptions); + return timeout(() => { nodeFetch(urlAdaptedForScraping, newOptions); }, randomPauseMS); }; module.exports = fetch;