Middle
/** * La fonction `asyncAuth(callback)` accepte un callback, dans lequel * une erreur peut être transmise (en premier argument) et des données * du backend (en second argument). * asyncAuth((error, data) => {}); * * Vous devez implémenter la fonction `auth()`, * qui appelle `asyncAuth()`, mais retourne une promesse. * * @returns {Promise} */ function auth() { // asyncAuth((error, data) => {}); } /** * La fonction `tryAuth()` utilise `auth()` et, en cas d'erreur, * effectue N tentatives supplémentaires. * Si toutes échouent, elle retourne la dernière erreur. * * @returns {Promise} */ function tryAuth(n) { }
sobes.tech IA
Réponse de l'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` раз в случае ошибки, и если все попытки неудачны, выбрасывает последнюю ошибку.