Skip to content

Instantly share code, notes, and snippets.

@st32lthx
Last active August 29, 2015 14:09
Show Gist options
  • Save st32lthx/7a23be9c57c0c71679a9 to your computer and use it in GitHub Desktop.
Save st32lthx/7a23be9c57c0c71679a9 to your computer and use it in GitHub Desktop.
Promises Basics in JavaScript
// 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;
}
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