I have the following code that works as expected:
(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';
}
})();
But I am curious whether there is a way to shorten the declaration of my variables, something like this (which doesn't work):
(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';
}
})();
Just curious as to whether I can get rid of that second appearance of 'document.getElementById' to make the line more compact.