|
| 1 | + |
| 2 | + |
| 3 | +(function(){ |
| 4 | + function changeBodyColor(){ |
| 5 | + return new Promise(function(resolve, reject){ |
| 6 | + var bodyElement = document.getElementsByTagName('body')[0]; |
| 7 | + bodyElement.className = 'soft-body'; |
| 8 | + resolve(bodyElement); |
| 9 | + }) |
| 10 | + } |
| 11 | + |
| 12 | + function addSubmitButtonToBody(bodyElement){ |
| 13 | + return new Promise(function(resolve, reject){ |
| 14 | + if (bodyElement){ |
| 15 | + var createdButton = document.createElement('button') |
| 16 | + createdButton.innerText = 'Submit'; |
| 17 | + createdButton.id = 'submit-button' |
| 18 | + bodyElement.appendChild(createdButton); |
| 19 | + resolve(bodyElement); |
| 20 | + } else { |
| 21 | + reject('No body available') |
| 22 | + } |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + function waitUntillButtonIsClicked(){ |
| 28 | + return new Promise(function(resolve, reject){ |
| 29 | + var button = document.getElementById('submit-button'); |
| 30 | + button.addEventListener('click', function(e){ |
| 31 | + var helloWorldTitle = document.querySelector('h1'); |
| 32 | + var successTitle = document.querySelector('h2'); |
| 33 | + |
| 34 | + helloWorldTitle.className = 'hidden'; |
| 35 | + successTitle.className = 'bolder'; |
| 36 | + resolve('Success!') |
| 37 | + }); |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + function showDelayedMessage(message){ |
| 42 | + return new Promise(function(resolve, reject){ |
| 43 | + setTimeout(function(){ |
| 44 | + alert('Hey hey! ' + message) |
| 45 | + resolve(); |
| 46 | + }, 5000); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + changeBodyColor() |
| 51 | + .then(function(bodyElement){ |
| 52 | + return addSubmitButtonToBody(bodyElement); |
| 53 | + }).then((res) => { |
| 54 | + return waitUntillButtonIsClicked(); |
| 55 | + }) |
| 56 | + .then((message) => { |
| 57 | + showDelayedMessage(message); |
| 58 | + }) |
| 59 | + .catch(e => { |
| 60 | + alert('AAAA' + e) |
| 61 | + }) |
| 62 | + |
| 63 | +})() |
0 commit comments