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
49 changes: 48 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
'use strict';

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

function createPromise1() {
return new Promise((resolve, reject) => {
resolve('Promise was resolved!');
});
}
Comment on lines +5 to +9

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

promise1 is required to be a top-level named promise (const promise1 = new Promise(...)) with its own .then and .catch handlers. Defining createPromise1() that creates and resolves the promise internally means promise1 is not a named top-level promise as specified.


logo.addEventListener('click', async () => {
try {
const promise1 = await createPromise1();
Comment on lines +11 to +13

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 click handler currently creates and resolves the promise via await createPromise1(). Per the requirements, the .logo click should only trigger resolution or rejection of an already-defined promise1, not create a new promise instance each time.


const division = document.createElement('div');

division.classList.add('message');
division.textContent = promise1;
document.body.appendChild(division);
Comment on lines +15 to +19

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 path for promise1 is implemented inside the click handler instead of as a .then attached directly to a top-level promise1. The checklist requires a .then on promise1 that appends <div class="message">Promise was resolved!</div> when the promise resolves.

} catch (error) {
const division = document.createElement('div');

division.classList.add('message',
'error-message');
division.textContent = error.message;
document.body.appendChild(division);
}
Comment on lines +20 to +27

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 error handling for promise1 is implemented via try/catch around the async function rather than a .catch handler attached to promise1 itself. Both promises must have explicit success and error handlers using .then and .catch.

});

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

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

division.classList.add('message');
division.textContent = result;
document.body.appendChild(division);
Comment on lines +36 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In the promise2 success handler, the description requires the success handler to append a <div class="message"> with text exactly Promise was resolved!. Using result here will not satisfy the requirement if the promise is ever resolved.

});

promise2.catch((error) => {
const division = document.createElement('div');

division.classList.add('message', 'error-message');
division.textContent = error.message;
Comment on lines +44 to +48

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In the promise2 rejection handler, the text content must be exactly Promise was rejected! per the description, not error.message. Use the fixed string regardless of the error object.

document.body.appendChild(division);
});
Loading