Last active
March 29, 2016 15:06
-
-
Save AlexDisler/93d11ba4ea787fdb067e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RowNotFound extends Error { | |
constructor() { | |
const message = `Row not found`; | |
super(message); | |
this.message = message; | |
this.name = 'RowNotFound'; | |
} | |
} | |
function checkRowExists(row) { | |
if (!row) return Promise.reject(new RowNotFound()) | |
return row; | |
} | |
function update(_id) { | |
return User | |
.findOneAndUpdate({ _id }, { $set: { updated: true } }, { new: true }) | |
.exec() | |
.then(checkRowExists) | |
.then(sendToView) | |
} | |
// If you reject the promise with an error instead of a string, you can see the stacktrace (to see what triggered it) and check what kind of error it is | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject | |
// http://www.devthought.com/2011/12/22/a-string-is-not-an-error/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment