Week1 UsingAPIs: solve all 5 exercises#15
Week1 UsingAPIs: solve all 5 exercises#15Hadidreem17 wants to merge 3 commits intoHackYourAssignment:mainfrom
Conversation
remarcmij
left a comment
There was a problem hiding this comment.
Hi @Hadidreem17, there are some issues that I would like you to have a look at. Please let me know on Slack if you need help with fixing them.
| getAnonName('John', console.log); | ||
| getAnonName('John') | ||
| .then(console.log) | ||
| .catch(err => console.error(err.message)); |
|
|
||
| checkDoubleDigits(100) // should reject | ||
| .then((message) => console.log(message)) | ||
| .catch((error) => console.log(error.message)); |
| } else { | ||
| reject(new Error(`Expected a double digit number but got ${number}`)); | ||
| } | ||
| }); |
|
|
||
| if (roll > 6) { | ||
| reject(new Error('Oops... Die rolled off the table.')); | ||
| return; |
There was a problem hiding this comment.
The exercise description states that the die should complete its scheduled rolls, even if the die rolls off the table. The return statement that you added here breaks off the scheduled rolls. So, for this exercise, remove the return statement and explain what happens.
Current output example (only 7 rolls of the scheduled 10 rolls):
Die scheduled for 10 rolls...
Die value is now: 2
Die value is now: 3
Die value is now: 1
Die value is now: 4
Die value is now: 4
Die value is now: 4
Die value is now: 1
Oops... Die rolled off the table.
Expected output example (all 10 rolls completed):
Die scheduled for 10 rolls...
Die value is now: 2
Die value is now: 3
Die value is now: 1
Die value is now: 4
Die value is now: 4
Die value is now: 4
Die value is now: 1
Oops... Die rolled off the table.
Die value is now: ...
Die value is now: ...
Die value is now: ...
|
|
||
| if (roll === randomRollsToDo) { | ||
| resolve(value); | ||
| return; |
| main(); | ||
| } | ||
|
|
||
| // TODO Replace this comment by your explanation that was asked for in the assignment description. |
There was a problem hiding this comment.
Where is your explanation that was asked here?
| const dicePromises = dice.map((die) => rollDie(die)); | ||
|
|
||
|
|
||
| return Promise.all(dicePromises); |
| return rollDie(1); | ||
|
|
||
|
|
||
| const dicePromises = dice.map((die) => rollDie(die)); |
| main(); | ||
| } | ||
|
|
||
| // TODO Replace this comment by your explanation that was asked for in the assignment description. |
| export function rollDice() { | ||
| const results = []; | ||
|
|
||
| // TODO: expand the chain to include five dice |
There was a problem hiding this comment.
A chain was asked for, not a Promise.all(). Please fix this.
remarcmij
left a comment
There was a problem hiding this comment.
Hi @Hadidreem17, thanks for the updates. I will now approve your PR.
| promise has been rejected because the die "rolled off the table". This matches | ||
| the exercise requirement: all scheduled rolls should complete, while the promise | ||
| still settles as a rejection at the moment of the "off the table" event. | ||
| */ |
There was a problem hiding this comment.
The actual question was to explain how the promise version solves the problem observed with the callback version. (You added the return statement, it was not in the original code and it was not intended for you to add it.) The promise version solves it because once a promise is settled its outcome can no longer be changed. So calling resolve() after reject() does not change a rejected promise to a resolved promise.
| and provides an array with all their results. | ||
| If any one die rejects (for example, rolls off the table), | ||
| the entire `Promise.all()` rejects immediately with that error. | ||
| */ |
There was a problem hiding this comment.
A rejected Promsise.all() does not stop the other still running promises.
| This ensures the dice are rolled sequentially, not in parallel. | ||
| If any die rejects (for example, it falls off the table), | ||
| the chain stops immediately and the final Promise rejects with that error. | ||
| */ |
No description provided.