Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feras Aldmeashki-w2-usingAPIs #69

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Feras-ALdemashki
Copy link

No description provided.

@remarcmij remarcmij self-assigned this Feb 5, 2025
Copy link

@remarcmij remarcmij left a comment

Choose a reason for hiding this comment

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

Hi @Feras-ALdemashki, I have identified some issues for you to address. The major one is in the pokemon exercise, where you should only fetch the pokemons after pressing the button. The other issues are minor. Please have a look at my comments below.

@@ -17,28 +19,31 @@ Full description at: https://github.com/HackYourFuture/Assignments/blob/main/3-U
should result in a network (DNS) error.
------------------------------------------------------------------------------*/
function requestData(url) {
// TODO return a promise using `fetch()`
return fetch(url).then((Response) => {

Choose a reason for hiding this comment

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

  1. Since this week is about using async/await, can you refactor this to use that?
  2. Variable names should start with a lowercase letter (camelcase): Response -> response.
  3. Don't forget to check response.ok.

// TODO complete this function
async function main() {
try {
const response = await fetchData(

Choose a reason for hiding this comment

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

You should fetch the pokemons only when the button is pressed, not at immediately as you are doing here, see the instructions:

Use fetchData() to load the pokemon data on demand (i.e. when the button is pressed) from the public API and populate the element in the DOM.

Copy link
Author

Choose a reason for hiding this comment

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

Hello Jim, i did all the other changes but I'm not sure if i understand this one I'm getting the same result in the description . As now i fetch the data and when the button is clicked i append it to the drop down isn't the same ? or I'm missing something

Choose a reason for hiding this comment

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

You are fetching too soon. You should fetch only when the button is pressed (i.e. on demand) because that was how it was specified in the assignment description.

Use fetchData() to load the pokemon data on demand (i.e. when the button is pressed)

Copy link
Author

Choose a reason for hiding this comment

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

i changed the solution now the fetchData( ) function will work when the button is clicked

Comment on lines 35 to 37
} catch (error) {
throw new Error('fetch failed');
}

Choose a reason for hiding this comment

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

There is no added value in catching an error here if the only thing that you are doing is rethrowing another error. Not every function that uses async/await needs a try/catch block if there already is a try/catch block higher up in the call chain that handles the error.

Comment on lines +77 to +78
const response = await fetch(pokemonUrl);
const pokemonData = await response.json();

Choose a reason for hiding this comment

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

Why not call he reusable fetchData() function here that already deals with checking response.ok etc.

Choose a reason for hiding this comment

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

You didn't address this point.

Comment on lines 28 to 30
} catch (error) {
throw new Error(error);
}

Choose a reason for hiding this comment

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

Useless try/catch here. You already have a try/catch block in the calling main() function.

Comment on lines 25 to 26
const result = await rollDice();
console.log('Resolved!', results);

Choose a reason for hiding this comment

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

When I run your code I get an undefined:

Rejected! results is not defined

Can you spot the typo?

}

// ! Do not change or remove the code below
if (process.env.NODE_ENV !== 'test') {
main();
}

// TODO Replace this comment by your explanation that was asked for in the assignment description.
// promise.race does not cancel the other promises, the other promises will keep going but we will not get their results as we get the first promise result

Choose a reason for hiding this comment

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

Correct

const birthPlace = birth?.place?.locationString?.en || 'Unknown';
console.log(`Birth: ${birthDate}, ${birthPlace}`);

const deathDate = death?.date || 'unknown';

Choose a reason for hiding this comment

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

While checking for undefined properties and replacing it with unknown is a good strategy in general, in the case of the death.date not being defined this usually means that the person is not dead. By using unknown you are leaving the possibility open that the person might be dead. That might not always be appreciated 😉 .

} catch (err) {
console.error(`Something went wrong: ${err.message}`);
console.error(err);

Choose a reason for hiding this comment

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

Why did you think this change was appropriate?

);

if (death) {
addTableRow(

Choose a reason for hiding this comment

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

This is a better approach to dealing with an undefined death property.

Copy link

@remarcmij remarcmij left a comment

Choose a reason for hiding this comment

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

Hi @Feras-ALdemashki, thanks for the updates. I still have some comments but they are minor therefore I'm happy to approve your PR now.

Comment on lines +34 to +36
} catch (error) {
console.error(error);
}

Choose a reason for hiding this comment

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

If fetch() throws an error you get it here and print an error message on the console. But after that the async fetchData() function returns a promise that resolves to the value undefined. It doesn't return a rejected promise because you have already "consumed" the error here. Therefore, the catch clause of the button event handler will not be triggered.

It is better to get rid of the try/catch blocks here and let the calling function handle the error (in your case, the event handler of the button element).

const pokemons = await fetchData(
'https://pokeapi.co/api/v2/pokemon?limit=151'
);
pokemons.forEach((pokemon) => {

Choose a reason for hiding this comment

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

If an error occurs in fetchData() the pokemons variable will be set to undefined, because you have already "consumed" the error in fetchData().

Comment on lines +77 to +78
const response = await fetch(pokemonUrl);
const pokemonData = await response.json();

Choose a reason for hiding this comment

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

You didn't address this point.

}

function main() {
// TODO complete this function
fetchAndPopulatePokemons();

Choose a reason for hiding this comment

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

It would seem more logical (given the name fetchAndPopulatePokemons() to only call this function when you are actually doing what the name suggests. Tha would imply that you only call that function when the button is pressed. It would require that move the event handler for the button the main() function.

document.body.appendChild(selectElement);

buttonElement.addEventListener('click', async () => {
selectElement.innerHTML = '';

Choose a reason for hiding this comment

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

Excellent to clear out the existing content before appending new content. Otherwise the list will get longer and longer if the button is pressed more than once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants