Skip to content

Instantly share code, notes, and snippets.

@Pyrolistical
Created April 29, 2016 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pyrolistical/446ea0f5625c53f002a9ba22161ebfab to your computer and use it in GitHub Desktop.
Save Pyrolistical/446ea0f5625c53f002a9ba22161ebfab to your computer and use it in GitHub Desktop.
example how to reduce nesting
const getUserByEmail = (connection, userEmail) => {
const userPromise = connection
.queryAsync(‘SELECT `id` FROM `user` WHERE `email` = ?’, [
userEmail
])
.then(R.head);
const permissionsPromise = userPromise
.then(R.unless(R.isNil, (user) => connection
.queryAsync(‘SELECT `id`, `name` FROM `permission` WHERE `user_id` = ?’, [
user.id
])));
return Promise.all([
userPromise,
permissionPromise
])
.then(({user, permission}) => {
if (!user) {
return null;
}
return {
user,
permissions
};
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment