-
Notifications
You must be signed in to change notification settings - Fork 4
Majd w1 using ap is #12
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,24 +7,27 @@ Rewrite this function, but replace the callback syntax with the Promise syntax: | |
| - If the Promise `rejects`, pass an error as the argument to reject with: "You | ||
| didn't pass in a first name!" | ||
| ------------------------------------------------------------------------------*/ | ||
| // TODO see above | ||
| export const getAnonName = (firstName, callback) => { | ||
| setTimeout(() => { | ||
| if (!firstName) { | ||
| callback(new Error("You didn't pass in a first name!")); | ||
| return; | ||
| } | ||
| export const getAnonName = (firstName) => { | ||
| return new Promise((resolve, reject) => { | ||
| setTimeout(() => { | ||
| if (!firstName) { | ||
| reject(new Error("You didn't pass in a first name!")); | ||
| return; | ||
| } | ||
|
|
||
| const fullName = `${firstName} Doe`; | ||
|
|
||
| callback(fullName); | ||
| }, 1000); | ||
| const fullName = `${firstName} Doe`; | ||
| resolve(fullName); | ||
| }, 1000); | ||
| }); | ||
| }; | ||
|
|
||
| function main() { | ||
| getAnonName('John', console.log); | ||
| getAnonName('John') | ||
| .then(console.log) | ||
| .catch(console.error); | ||
|
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job separating the success output and the error output. |
||
| } | ||
|
|
||
|
|
||
| // ! Do not change or remove the code below | ||
| if (process.env.NODE_ENV !== 'test') { | ||
| main(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,14 @@ Complete the function called `checkDoubleDigits` such that: | |
| "Expected a double digit number but got `number`", where `number` is the | ||
| number that was passed as an argument. | ||
| ------------------------------------------------------------------------------*/ | ||
| export function checkDoubleDigits(/* TODO add parameter(s) here */) { | ||
| // TODO complete this function | ||
| export function checkDoubleDigits(number) { | ||
| return new Promise((resolve, reject) => { | ||
| if (number >= 10 && number <= 99) { | ||
| resolve("This is a double digit number!"); | ||
| } else { | ||
| reject(new Error(`Expected a double digit number but got ${number}`)); | ||
| } | ||
| }); | ||
|
Comment on lines
+14
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good! Meets all the requirements. 👍 |
||
| } | ||
|
|
||
| function main() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,9 @@ Full description at: https://github.com/HackYourFuture/Assignments/tree/main/3-U | |
| explanation? Add your answer as a comment to be bottom of the file. | ||
| ------------------------------------------------------------------------------*/ | ||
|
|
||
| // TODO Remove callback and return a promise | ||
| export function rollDie(callback) { | ||
| export function rollDie() { | ||
| return new Promise((resolve, reject) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| // Compute a random number of rolls (3-10) that the die MUST complete | ||
| const randomRollsToDo = Math.floor(Math.random() * 8) + 3; | ||
| console.log(`Die scheduled for ${randomRollsToDo} rolls...`); | ||
|
|
@@ -23,40 +24,47 @@ export function rollDie(callback) { | |
|
|
||
| // Use callback to notify that the die rolled off the table after 6 rolls | ||
| if (roll > 6) { | ||
| // TODO replace "error" callback | ||
| callback(new Error('Oops... Die rolled off the table.')); | ||
| reject(new Error('Oops... Die rolled off the table.')); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return; | ||
| } | ||
|
|
||
| // Use callback to communicate the final die value once finished rolling | ||
| if (roll === randomRollsToDo) { | ||
| // TODO replace "success" callback | ||
| callback(null, value); | ||
| resolve(value); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return; | ||
| } | ||
|
|
||
| // Schedule the next roll todo until no more rolls to do | ||
| if (roll < randomRollsToDo) { | ||
| if (roll < randomRollsToDo) { | ||
| setTimeout(() => rollOnce(roll + 1), 500); | ||
| } | ||
| }; | ||
|
|
||
| // Start the initial roll | ||
| rollOnce(1); | ||
| }); | ||
| } | ||
|
|
||
| function main() { | ||
| // TODO Refactor to use promise | ||
| rollDie((error, value) => { | ||
| if (error !== null) { | ||
| console.log(error.message); | ||
| } else { | ||
| console.log(`Success! Die settled on ${value}.`); | ||
| } | ||
| }); | ||
| rollDie() | ||
| .then(value => console.log(`Success! Die settled on ${value}.`)) | ||
| .catch(error => console.log(error.message)); | ||
|
Comment on lines
+49
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } | ||
|
|
||
| // ! 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. | ||
| /*Explanation: | ||
|
|
||
| Before, using callbacks caused a problem because when the die rolled off the table or finished rolling, the messages could happen in a random order | ||
|
|
||
| Now, using a Promise | ||
|
|
||
| 1. We call `resolve()` when the die finishes rolling successfully. | ||
| 2. We call `reject()` if the die rolls off the table (more than 6 rolls) | ||
|
|
||
| Promises make sure only one thing happens: either success or error. We can handle it easily with `.then()` for success and `.catch()` for errors. | ||
|
|
||
| Because of this, the previous problem does not happen anymore, and everything runs in a clear predictable order */ | ||
|
Comment on lines
+59
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,11 +27,12 @@ exercise file. | |
| import { rollDie } from '../../helpers/pokerDiceRoller.js'; | ||
|
|
||
| export function rollDice() { | ||
| // TODO Refactor this function | ||
| const dice = [1, 2, 3, 4, 5]; | ||
| return rollDie(1); | ||
| } | ||
|
|
||
| const dicePromises = dice.map(() => rollDie()); | ||
| return Promise.all(dicePromises); | ||
| } | ||
|
|
||
|
Comment on lines
+32
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job using Promise.all() |
||
| function main() { | ||
| rollDice() | ||
| .then((results) => console.log('Resolved!', results)) | ||
|
|
@@ -43,4 +44,14 @@ if (process.env.NODE_ENV !== 'test') { | |
| main(); | ||
| } | ||
|
|
||
| // TODO Replace this comment by your explanation that was asked for in the assignment description. | ||
| /* | ||
| Explanation: | ||
|
|
||
| We throw five dice at the same time using Promise.all. Each die is like a separate promise. | ||
| - If all dice finish rolling without problems, we get an array with all the results. | ||
| - If one die falls off the table, Promise.all stops and gives an error. | ||
|
|
||
| Even if one die fails, the other dice keep rolling. | ||
| This happens because each die rolls in its own process, and Promise.all cannot stop them once they started. | ||
| It only stops the main result from coming, but the dice themselves keep going until they finish. | ||
| */ | ||
|
Comment on lines
+47
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well done |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,22 +10,34 @@ To throw the dice sequentially we will be using a _promise chain_. Your job is | |
| to expand the given promise chain to include five dice. | ||
| ------------------------------------------------------------------------------*/ | ||
|
|
||
|
|
||
| // The line below makes the rollDie() function available to this file. | ||
| // Do not change or remove it. | ||
| import { rollDie } from '../../helpers/pokerDiceRoller.js'; | ||
|
|
||
| export function rollDice() { | ||
| const results = []; | ||
|
|
||
| // TODO: expand the chain to include five dice | ||
| return rollDie(1) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return rollDie(2); | ||
| }) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return rollDie(3); | ||
| }) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return rollDie(4); | ||
| }) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return results; | ||
| return rollDie(5); | ||
| }) | ||
| .then((value) => { | ||
| results.push(value); | ||
| return results; // Return all results at the end | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -39,3 +51,13 @@ function main() { | |
| if (process.env.NODE_ENV !== 'test') { | ||
| main(); | ||
| } | ||
| /* | ||
| Explanation: | ||
|
|
||
| We throw five dice one by one, waiting for each to finish before throwing the next. | ||
| - If a die finishes normally, its value is added to the results array. | ||
| - If a die rolls off the table, the chain stops immediately and the error is caught. | ||
|
|
||
| This is different from Promise.all(), because here each die waits for the previous one. | ||
| Even though a die falls off, any dice that haven't started yet never roll. | ||
| */ | ||
|
Comment on lines
+54
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good! |
||
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.
Great job writing clean code that returns a promise, and processing resolve and reject.