-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: main
Are you sure you want to change the base?
Feras Aldmeashki-w2-usingAPIs #69
Conversation
There was a problem hiding this 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) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Since this week is about using
async/await
, can you refactor this to use that? - Variable names should start with a lowercase letter (camelcase):
Response
->response
. - Don't forget to check
response.ok
.
// TODO complete this function | ||
async function main() { | ||
try { | ||
const response = await fetchData( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
} catch (error) { | ||
throw new Error('fetch failed'); | ||
} |
There was a problem hiding this comment.
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.
const response = await fetch(pokemonUrl); | ||
const pokemonData = await response.json(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
} catch (error) { | ||
throw new Error(error); | ||
} |
There was a problem hiding this comment.
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.
const result = await rollDice(); | ||
console.log('Resolved!', results); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
} catch (error) { | ||
console.error(error); | ||
} |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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()
.
const response = await fetch(pokemonUrl); | ||
const pokemonData = await response.json(); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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 = ''; |
There was a problem hiding this comment.
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.
No description provided.