Gunnar Bittersmann: asynchrones JavaScript

Beitrag lesen

@@Gunnar Bittersmann

Ich mach mal den Columbo: Eine Frage hätte ich dann doch noch.

Im Script kann man jedes var durch let ersetzen. Aber was wäre wirklich stilvoll? Wo wäre var angebracht, wo let?

var currentLanguage = document.documentElement.lang;

var urls = [
	'config/available-languages.json',
	'lib/language-names.json',
	'lib/language-names.' + currentLanguage + '.json'
];

Promise.all(
	urls.map(
		url => fetch(url).then(response => response.json())
	)
).then(values => {
	
	var [availableLanguages, languageNames, localLanguageNames] = values;
	var template = document.querySelector('#lang-menu-item');

	availableLanguages.forEach(lang => {
		
		if (lang != currentLanguage)
		{
			var clone = template.content.cloneNode(true);
			var liElement = clone.querySelector('li');
			var aElement = liElement.querySelector('a');
			
			aElement.href = '?lang=' + lang;
			aElement.hreflang = lang;
			aElement.lang = lang;
			aElement.textContent = languageNames[lang] || lang;
			liElement.title = localLanguageNames[lang] || '';
			
			template.parentNode.appendChild(clone);
		}
	});
});

LLAP 🖖

--
“When UX doesn’t consider all users, shouldn’t it be known as ‘Some User Experience’ or... SUX? #a11y” —Billy Gregory