Instantly share code, notes, and snippets.

What would you like to do?
function addComment(comment) {
return fetchPostBySlug(comment.postSlug)
.then((post) => {
if (!post.isCommentingEnabled) {
return Promise.reject(new Error('Commenting is disabled.'));
return isUserAllowedToComment(post, comment.user)
.then((isAllowed) => ({ isAllowed, post }));
}).then((input) => {
if (!input.isAllowed) {
return Promise.reject(
new Error('The user is not allowed to comment on this post.')
return insertComment(, comment.user, comment.message);
}).then((result) => {
return result.record;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment