Created
May 14, 2018 15:55
-
-
Save glepur/0304e6d57fc2bd68d28ea7a78aedf5dd to your computer and use it in GitHub Desktop.
Improved promise and error handling in nodejs.
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
| //*** HELPER FUNCTIONS *** | |
| //*** npm install parse-error | |
| pe = require('parse-error'); | |
| to = function(promise) { | |
| return promise | |
| .then(data => { | |
| return [null, data]; | |
| }).catch(err => | |
| [pe(err)] | |
| ); | |
| }; | |
| TE = function(err_message, log){ // TE 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