Sobes.tech
Middle

/** * La función `asyncAuth(callback)` acepta un callback, en el cual * puede pasarse un error (como primer argumento) y datos * del backend (como segundo argumento). * asyncAuth((error, data) => {}); * * Necesitas implementar la función `auth()`, * que llama a `asyncAuth()`, pero devuelve una Promesa. * * @returns {Promise} */ function auth() { // asyncAuth((error, data) => {}); } /** * La función `tryAuth()` usa `auth()` y, en caso de error, * realiza N intentos adicionales. * Si todos los intentos fallan, devuelve el último error. * * @returns {Promise} */ function tryAuth(n) { }

sobes.tech AI

Respuesta de la IA

javascript function auth() { return new Promise((resolve, reject) => { asyncAuth((error, data) => { if (error) { reject(error); } else { resolve(data); } }); }); }

async function tryAuth(n) { let lastError; for (let i = 0; i <= n; i++) { try { const result = await auth(); return result; } catch (error) { lastError = error; if (i === n) { throw lastError; } } } }


Функция `auth()` оборачивает вызов `asyncAuth` в Promise, чтобы использовать современный синтаксис async/await. Функция `tryAuth(n)` пытается вызвать `auth()` до `n` раз в случае ошибки, и если все попытки неудачны, выбрасывает последнюю ошибку.