Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
mensaje de error No capturado (en promesa), pero no puedo encontrar el problema después de agregar la función de captura

¿Por qué la captura agregada todavía no funciona en absoluto?

 function maxRequest(url = ``, times = 3) { // closure function autoRetry (url, times) { console.log('times = ', times); times--; return new Promise((resolve, reject) => { fetch(url).then(value => { if(value.status === 200) { console.log(`✅ `, value); resolve(value); } else { throw new Error(`❌ http code error: ${value.status }`); } }).catch((err) => { console.log(`❌ Error`, err); if (times < 1) { reject('💩 over max request times!'); } else { autoRetry(url, times); } }); }); } return autoRetry(url, times); } maxRequest(`https://cdn.xgqfrms.xyz/json/badges.js`) .then(res => res.json()) .then(json => { console.log('json =', json); return json; }, err => { console.log('error =', err); throw new Error(err); }) .catch(err => console.log(`err =`, err)) .finally(() => { console.log('whatever close loading...'); });

ingrese la descripción de la imagen aquí

ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Su uso del antipatrón de construcción Promise explícito enmascara el hecho de que no regresa de su bloque catch . En una situación de reintento, crea una Promesa de segundo intento llamando a autoRetry , pero no hace nada con ella, por lo que el bloque catch en su llamada a maxRequest no lo protege y termina con una Promesa rechazada no detectada.

 function maxRequest(url = ``, times = 3) { // closure function autoRetry (url, times) { console.log('times = ', times); times--; // No more `new Promise`, just a chain. return fetch(url).then(value => { if(value.status === 200) { console.log(`✅ `, value); return value; } else { throw new Error(`❌ http code error: ${value.status }`); } }).catch((err) => { console.log(`❌ Error`, err); if (times < 1) { // Converted to rejection. You could still throw a // string, but throwing an Error is more idiomatic. throw new Error('💩 over max request times!'); } else { // Make sure to return your Promise here. Don't worry // about returning a Promise; it's automatically wrapped // like Promise.resolve() does, so this chained-fetch() // Promise eventually gets the same result as the returned // recursive Promise. return autoRetry(url, times); } }); } return autoRetry(url, times); } maxRequest(`https://cdn.xgqfrms.xyz/json/badges.js`) .then(res => res.json()) .then(json => { console.log('json =', json); return json; }, err => { console.log('error =', err); throw new Error(err); }) .catch(err => console.log(`err =`, err)) .finally(() => { console.log('whatever close loading...'); });

En aras de la legibilidad, es posible que también desee cambiar los nombres de las variables, por lo que los times no se usan tanto para el ámbito externo como para el interno. También puede reformular esto como una función async mucho más corta que se repite.

ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!