diff --git a/README.md b/README.md index 1395a36c..9a3f65d2 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://OleksiiHolodok.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..9fc5dce5 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,38 @@ 'use strict'; -// write your code here +document.addEventListener('DOMContentLoaded', () => { + const logo = document.querySelector('.logo'); + + const showSuccess = () => { + const div = document.createElement('div'); + + div.classList.add('message'); + div.textContent = 'Promise was resolved!'; + document.body.appendChild(div); + }; + + const showError = () => { + const div = document.createElement('div'); + + div.classList.add('message', 'error-message'); + div.textContent = 'Promise was rejected!'; + document.body.appendChild(div); + }; + + const promise1 = new Promise((resolve) => { + logo.addEventListener('click', () => { + resolve(); + }); + }); + + const promise2 = new Promise((resolve, reject) => { + void resolve; + + setTimeout(() => { + reject(new Error('Promise was rejected')); + }, 3000); + }); + + promise1.then(showSuccess).catch(showError); + promise2.then(showSuccess).catch(showError); +});