diff --git a/README.md b/README.md index 1395a36c..fc4935fa 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Create 2 promises `promise1` and `promise2`. ## Instructions 1. Replace `` with your Github username in the link - - [DEMO LINK](https://.github.io/js_promise_basic_DOM/) + - [DEMO LINK](https://fedechkova.github.io/js_promise_basic_DOM/) 2. Follow [this instructions](https://mate-academy.github.io/layout_task-guideline/) - Run `npm run test` command to test your code; - Run `npm run test:only -- -n` to run fast test ignoring linter; diff --git a/src/scripts/main.js b/src/scripts/main.js index c6e3f878..6cd6e12c 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,34 @@ 'use strict'; -// write your code here +const body = document.querySelector('body'); +const logo = document.querySelector('.logo'); + +const promise1 = new Promise((resolve) => { + logo.addEventListener('click', () => { + resolve(); + }); +}); + +promise1.then(handleSuccess).catch(handleError); + +const promise2 = new Promise((resolve, reject) => { + setTimeout(() => { + reject(new Error('Something went wrong')); + }, 3000); +}); + +promise2.then(handleSuccess).catch(handleError); + +function handleSuccess() { + const successHandler = body.appendChild(document.createElement('div')); + + successHandler.classList.add('message'); + successHandler.textContent = 'Promise was resolved!'; +} + +function handleError() { + const errorHandler = body.appendChild(document.createElement('div')); + + errorHandler.classList.add('message', 'error-message'); + errorHandler.textContent = 'Promise was rejected!'; +}