Last active
August 29, 2015 14:09
-
-
Save st32lthx/7a23be9c57c0c71679a9 to your computer and use it in GitHub Desktop.
Promises Basics in JavaScript
This file contains 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
// add some basic error handling | |
var q = require("q"); | |
var fs = require("fs"); | |
function qread(file) { | |
var _q = q.defer(); | |
fs.readFile(file, "utf-8", function (err, data) { | |
if (err) { _q.reject(new Error(err)) | |
} else { _q.resolve(data); } | |
}); | |
return _q.promise; | |
} |
This file contains 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"); | |
var fs = require("fs"); | |
// wrap fs.read in a promise | |
function qread(file) { | |
var _q = q.defer(); | |
fs.readFile(file, "utf-8", function (err, data) { _q.resolve(data) }); | |
return _q.promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment