Last active
December 20, 2015 13:19
-
-
Save AmineS/6137435 to your computer and use it in GitHub Desktop.
Is Q the answer for our error handling?
p.s if any method in the chain throws an error it's caught it at the end by fail. This means that you don't need to worry about error handling at every step.
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
| var Q = require('q') | |
| , fs = require('fs'); | |
| function add (num1, num2) { | |
| return num1 + num2; | |
| } | |
| function triple (num) { | |
| return num * f3(); | |
| } | |
| function divBy2 (num) { | |
| return num / 2; | |
| } | |
| function f3 () { | |
| return 3; | |
| // throw new Error("ERR"); | |
| } | |
| function read () | |
| { | |
| var deferred = Q.defer(); | |
| fs.readFile("/Users/Pepe/sequellizeTest/testQ.js", "utf-8", function (error, text) { | |
| if (error) { | |
| deferred.reject(new Error(error)); | |
| } else { | |
| deferred.resolve(text); | |
| } | |
| }); | |
| return deferred.promise; | |
| } | |
| Q.fcall(add, 1, 2) | |
| .then(function (val) { | |
| return triple(val); | |
| }) | |
| .then(function (val) { | |
| return divBy2(val); | |
| }) | |
| .then(read) | |
| .then(function (value) { | |
| console.log(value); | |
| }) | |
| .fail(function (err) { | |
| console.log(err); | |
| }) | |
| .done(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment