Created
February 16, 2017 03:59
-
-
Save lubien/9dbd3b8050fff19f77d7fd0b9b060740 to your computer and use it in GitHub Desktop.
Promise races
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const $page = document.getElementById('page') | |
let currentPage = 'no-page' | |
const setPage = title => { | |
if (currentPage !== title) return; | |
$page.innerText = title | |
} | |
const loadPage = (title, time) => | |
new Promise(resolve => | |
setTimeout(() => resolve(title), time) | |
) | |
const onClickChangePage = button => | |
button.addEventListener('click', () => { | |
const nextPage = button.getAttribute('data-page') | |
const delay = +button.getAttribute('data-delay') | |
currentPage = nextPage | |
loadPage(nextPage, delay) | |
.then(setPage) | |
}) | |
Array.from(document.getElementsByTagName('button')) | |
.map(onClickChangePage) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment