Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
'use strict';

// write your code here
const body = document.querySelector('body');

const logo = document.querySelector('.logo');

const promise1 = new Promise((resolve, reject) => {
logo.addEventListener('click', () => {
resolve();
});
});

const promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Promise was rejected'));
}, 3000);
});

promise1
.then(() => {
const div = document.createElement('div');

div.classList.add('message');
div.textContent = 'Promise was resolved!';
body.append(div);
})
.catch(() => {
const div = document.createElement('div');

div.classList.add('message', 'error-message');
div.textContent = 'Promise was rejected!';
body.append(div);
});

promise2
.then(() => {
const div = document.createElement('div');

div.classList.add('message');
div.textContent = 'Promise was resolved!';
body.append(div);
})
.catch(() => {
const div = document.createElement('div');

div.classList.add('message', 'error-message');
div.textContent = 'Promise was rejected!';
body.append(div);
});
Comment on lines +19 to +49
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The success and error handling logic is repeated for both promise1 and promise2. To make the code cleaner and more maintainable, consider creating two separate functions: one to handle successful promise resolution and one for rejection. You can then pass these functions as arguments to .then() and .catch() for both promises, avoiding code duplication.