Skip to content

add solution#1362

Open
ATkachenkoA wants to merge 3 commits intomate-academy:masterfrom
ATkachenkoA:develop
Open

add solution#1362
ATkachenkoA wants to merge 3 commits intomate-academy:masterfrom
ATkachenkoA:develop

Conversation

@ATkachenkoA
Copy link
Copy Markdown

- [DEMO LINK](https://ATkachenkoA.github.io/js_promises_practice_DOM/)

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Great work on implementing the promise logic! The secondPromise and notification handlers are working perfectly. However, a couple of changes are needed before this can be approved.

The main issue is with firstPromise. Using { once: true } causes the event listener to be removed after any mouse click. If a user right-clicks first, the listener is detached, and a subsequent left-click will never resolve the promise, causing it to incorrectly reject. The listener should only be removed once the promise is settled.

Additionally, for thirdPromise, the event listener is never removed after the promise resolves. While this doesn't break the functionality, it's inefficient. It's a good practice to clean up listeners using removeEventListener once they are no longer needed to prevent memory leaks.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread src/scripts/main.js Outdated
resolve('First promise was resolved');
}
},
{ once: true },
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using { once: true } here introduces a bug. This option removes the listener after any mousedown event. If the first click is a right-click, the listener is removed, and the promise can no longer be resolved with a subsequent left-click. This will cause the promise to be rejected by the timeout, which doesn't meet the requirement that it should resolve on a left click.

Comment thread src/scripts/main.js Outdated
Comment on lines +39 to +51
document.addEventListener('mousedown', (e) => {
if (e.button === 0) {
leftClicked = true;
}

if (e.button === 2) {
rightClicked = true;
}

if (leftClicked && rightClicked) {
resolve('Third promise was resolved');
}
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This event listener is never removed, even after the promise has been resolved. This is inefficient as it will continue to run on every mouse click for the lifetime of the page. It's a good practice to clean up event listeners when they are no longer needed. Consider defining the listener as a named function and then removing it with removeEventListener right after calling resolve.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Excellent work on the updates, you're very close to a complete solution! The logic for handling clicks and cleaning up listeners has improved significantly.

However, there is one important issue remaining in firstPromise that requires changes. When the promise is rejected by the timeout, its mousedown event listener is never removed. This creates a memory leak because the listener remains active even after the promise is finished.

Please ensure the event listener is always cleaned up when the promise settles, both when it resolves successfully and when it is rejected. Once you fix this, your solution will be perfect!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread src/scripts/main.js Outdated

const firstPromise = new Promise((resolve, reject) => {
const timerId = setTimeout(() => {
reject(new Error('First promise was rejected'));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When this rejection happens, the firstClickHandler is still listening for mousedown events. You should also call document.removeEventListener('mousedown', firstClickHandler) here to clean it up and prevent a memory leak.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Excellent work on this submission! You've done a fantastic job addressing the previous feedback and correctly implementing the event listener cleanup for firstPromise. Your code is clean, efficient, and fully meets all the requirements for handling the promises and displaying notifications. This is a great implementation, and I am happy to approve your solution. Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants