Tengo el siguiente código que funciona como se esperaba:
(async () => { const first = document.getElementById("first"), second = document.getElementById("second"); try { first.innerHTML = await (await fetch('https://myAPI.com/1')).text(); second.innerHTML = await (await fetch('https://myAPI.com/2')).text(); } catch { first.innerHTML = 'Error'; second.innerHTML = 'Error'; } })();
Pero tengo curiosidad por saber si hay una forma de acortar la declaración de mis variables, algo como esto (que no funciona):
(async () => { const [first, second] = document.getElementById("[first, second]"); try { first.innerHTML = await (await fetch('https://myAPI.com/1')).text(); second.innerHTML = await (await fetch('https://myAPI.com/2')).text(); } catch { first.innerHTML = 'Error'; second.innerHTML = 'Error'; } })();
Solo tengo curiosidad por saber si puedo deshacerme de esa segunda aparición de 'document.getElementById' para hacer que la línea sea más compacta.
La forma más concisa de hacerlo es usando lo siguiente:
const [first, second] = document.querySelectorAll("#first, #second");