Hago solicitudes https usando una función batchRequest que recopila las promesas en lotes con Promise.allSettled y las envía a la función apiRequest que realiza las llamadas reales con axios.
En la función batchRequest , envío una matriz de direcciones URL a la función de solicitud HTTP y espero los resultados. Luego uso el status Promise.allSettled para identificar los rejected . Pero cuando apiRequest el error Promise.allSettled lo ve como status: fullfilled
async function batchRequest(data, index) { console.log(`Running... ${index} round`) while (data.length) { // Batched request according to concurrent setting const batch = data .splice(0, settings.concurrent) .map((url) => apiRequest(url, index)) const results = await Promise.allSettled(batch) results.forEach(({ status, value, reason }) => { if (status === 'fulfilled') { console.log(value) // I get both the try and catch here from `apiRequest` } if (status === 'rejected') { console.log(reason) // I never get anything here } }) } }Función de solicitud de API
async function apiRequest(url, index) { try { const { data } = await axios('https://api-end-point') return 'All good' + url } catch (error) { const status = error.response?.status ?? 'No response' return `Error: ${status}, ${url}` } }No devuelvas el error. ¡tírarlo!
async function apiRequest(url, index) { try { const { data } = await axios('https://api-end-point') return 'All good' + url } catch (error) { const status = error.response?.status ?? 'No response' throw `Error: ${status}, ${url}` // <------------change here } }