-
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
Volodymyr-w1-UsingAPIs #51
base: main
Are you sure you want to change the base?
Volodymyr-w1-UsingAPIs #51
Conversation
|
||
callback(fullName); | ||
}, 1000); | ||
export const getAnonName = (firstName) => { |
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 with your solution to this first exercise (using Promise
syntax instead of callbacks
). The logic inside the setTimeout function is clear and follows the expected flow.
export const getAnonName = (firstName) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
if (firstName) { |
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 looks great but a couple of things we can improve are:
-
prevent redundant return statement:
Thereturn
statement afterresolve(fullName)
is unnecessary because the resolve call ends the execution of the promise immediately. -
"Fail first" or "Fail Fast" approach:
It is sometimes good to identify and report errors as early as possible in our code or development cycle. The benefit is that, it helps prevent bugs from propagating and becoming more difficult and costly to fix later on.
Original solution:
...
setTimeout(() => {
if (firstName) {
const fullName = `${firstName} Doe`;
resolve(fullName);
return;
} else {
reject(new Error("You didn't pass in a first name!"));
}
}, 1000);
...
Alternative: Here, we handle the case where the promise reject first. In addition, since, we use return
statement in the if () {}
statement, we don't have to add else {}
block to our code.
...
setTimeout(() => {
if (!firstName) {
reject(new Error("You didn't pass in a first name!"));
return;
}
resolve(`${firstName} Doe`);
}, 1000);
...
@@ -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) { |
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.
Well done, this is well written. Kudos.
return rollDie(1) | ||
.then((value) => { | ||
results.push(value); | ||
return rollDie(2); | ||
}) | ||
.then((value) => { |
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, this works as expected.
But if you noticed, we are repeating these two codes from line 22 to 39:
results.push(value);
return rollDie(4);
We can prevent this with a Software engineering principle called "DRY Principle". "Don't Repeat Yourself" (DRY) is a software development principle that helps us to avoid duplicating code. If you aren't aware of it, you can Google Search.
|
||
### 3-UsingAPIs - Week1 | ||
|
||
| Exercise | Passed | Failed | ESLint | |
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.
@Volodymyr-coder Great job completing week 1 assignment. Overall, this is an impressive solution, and I can see that you put a lot of effort into it. Keep up the great work, and don't hesitate to ask if you have any questions or need further clarification on any of the points above. I will Approve it now.
No description provided.