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

Refactor(achievement): Rewritten Achievements helpers with async/await syntax #587

Merged
merged 12 commits into from
May 17, 2021

Conversation

akashgp09
Copy link
Contributor

Problem

This PR Fixes: BB-598

Solution

A lot of the code uses promises in the promise.then(…) format, which makes the code hard to read and maintain. Rewritten it using the async/await syntax

Areas of Impact

file src/server/helpers/achievement.js

akashgp09 and others added 4 commits March 31, 2021 22:12
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
@akashgp09 akashgp09 changed the title Refactor(achievement): Converted promise.then(...) to async/wait syntax Refactor(achievement): Rewritten Achievements helpers with async/await syntax Mar 31, 2021
Signed-off-by: Akash Gupta <akashgp9@gmail.com>
@coveralls
Copy link

coveralls commented Mar 31, 2021

Coverage Status

Coverage decreased (-0.02%) to 60.893% when pulling 70a304c on akashgp09:refactor-achievement into 2f6246b on bookbrainz:master.

Copy link
Contributor

@MonkeyDo MonkeyDo left a comment

Choose a reason for hiding this comment

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

Most fo the comments below are repeated, as the code itself is repetitive.
Overall that's a good conversion !

const unlock = await new UnlockType(awardAttribs).save(null, {method: 'insert'});
return unlock.toJSON();
}
return new Promise((resolve) => resolve('Already unlocked'));
Copy link
Contributor

Choose a reason for hiding this comment

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

This syntax is redundant, along with the same in the catch block.
Th async keyword already defines that the function returns a Promise, so instead of return new Promise([…] resolve(myReturnValue) you can simply return myReturnValue.

On the other side in the catch block you can simply throw err.

I'm not exactly sure why this code either returns an unlock object or the string 'Already unlocked', that seems a bit odd to me, but it's clear from the docs above that it's intended, so not an issue for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On the other side in the catch block you can simply throw err

I have removed the try catch statement as the catch clause was only rethrowing the original error, which is redundant.

src/server/helpers/achievement.js Outdated Show resolved Hide resolved
src/server/helpers/achievement.js Outdated Show resolved Hide resolved
src/server/helpers/achievement.js Outdated Show resolved Hide resolved
src/server/helpers/achievement.js Outdated Show resolved Hide resolved
src/server/helpers/achievement.js Outdated Show resolved Hide resolved
src/server/helpers/achievement.js Outdated Show resolved Hide resolved
src/server/helpers/achievement.js Outdated Show resolved Hide resolved
src/server/helpers/achievement.js Outdated Show resolved Hide resolved
src/server/helpers/achievement.js Outdated Show resolved Hide resolved
akashgp09 and others added 7 commits May 5, 2021 20:08
Co-authored-by: Monkey Do <MonkeyDo@users.noreply.github.com>
Co-authored-by: Monkey Do <MonkeyDo@users.noreply.github.com>
Co-authored-by: Monkey Do <MonkeyDo@users.noreply.github.com>
Co-authored-by: Monkey Do <MonkeyDo@users.noreply.github.com>
@akashgp09
Copy link
Contributor Author

@MonkeyDo thank you so much for the detailed review 💯 . I have refactored the code as suggested, do let me know if changes are needed :)

Copy link
Contributor

@MonkeyDo MonkeyDo left a comment

Choose a reason for hiding this comment

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

Thanks a bunch, the code is a lot more readable now ! :)

@MonkeyDo MonkeyDo merged commit deb9acf into metabrainz:master May 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants