Utilizo async y for loop para probar problemas de recuperación que pueden ser causados por la red u otros problemas. pero probar con demasiada frecuencia genera un error de solicitud similar a los pings interminables. Después de regresar, quiero esperar 3000 milisegundos antes de reiniciar la prueba. Este es mi código:
const fetchtest = async (url, opts, tries=FETCHTEST_LOOP) => { const sleep = m => new Promise(r => setTimeout(r, m)); const errs = []; for (let i = 0; i < tries; i++) { try { return await fetch(url, opts).then(sleep(3000)) ; // << When returned, wait 3000 ms to restart the test. } catch (err) { errs.push(err); } } throw errs; };este es mi registro:
𓊈2𓊉 369 update at actiVity trying GET [1 of 10] trying GET [2 of 10] trying GET [2 of 10] trying GET [1 of 10] trying GET [2 of 10] trying GET [1 of 10] trying GET [2 of 10] trying GET [4 of 10] trying GET [3 of 10] trying GET [2 of 10] trying GET [1 of 10] trying GET [3 of 10] trying GET [1 of 10] trying GET [2 of 10] trying GET [1 of 10] trying GET [2 of 10] trying GET [4 of 10] trying GET [3 of 10] trying GET [1 of 10] trying GET [2 of 10] trying GET [2 of 10] trying GET [2 of 10] trying GET [1 of 10] trying GET [3 of 10] trying GET [2 of 10] trying GET [1 of 10] trying GET [4 of 10] trying GET [2 of 10] trying GET [1 of 10] trying GET [2 of 10] trying GET [1 of 10] 𓊈2𓊉 370 update at actiVity trying GET [1 of 10] trying GET [2 of 10] 𓊈2𓊉 370 update at actiVity 𓊈2𓊉 370 update at actiVitySoy nuevo en esto.
Tengo una respuesta para esto. cambio a @vercel/fetch API, hace que no necesite usar setTimeout.
No sé exactamente lo que estás intentando, pero tal vez pueda darte una idea.
const fetchtest = async (url, opts, tries=FETCHTEST_LOOP) => { const errs = []; for (let i = 0; i < tries;) { try { const response = await fetch(url, opts); const data = response.json() if (data){ setTimeout(()=>{i++},3000) } } catch (err) { errs.push(err); setTimeout(()=>{i++},3000) } } throw errs; };no es súper elegante pero al menos espera 3 segundos antes de reiniciar el ciclo.
Nuevamente, no está claro qué es exactamente lo que quiere (me refiero al propósito de toda su tarea/prueba) y posiblemente haya una mejor manera de lograrlo.