Created
February 9, 2020 02:36
-
-
Save krfong916/70ce6a8635c31f9a07a2efbfe76f1925 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
| let saveUser = async function(){ | |
| let err, user; | |
| [err, user] = await to(UserModel.findById(1)); | |
| if(err) throwError(err.message, true); | |
| user.name = “this rocks” | |
| [err, user] = await to(user.save()); | |
| if(err) throwError(‘error on saving user’); | |
| return user; | |
| } | |
| //*** HELPER FUNCTIONS *** | |
| //*** npm install parse-error | |
| pe = require('parse-error'); | |
| to = function(promise) { | |
| return promise | |
| .then(data => { | |
| return [null, data]; | |
| }).catch(err => | |
| [pe(err)] | |
| ); | |
| } | |
| throwError = function(err_message, log){ // throwError stands for Throw Error | |
| if(log === true){ | |
| console.error(err_message); | |
| } | |
| throw new Error(err_message); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment