Skip to content

Instantly share code, notes, and snippets.

@medikoo
Created June 30, 2011 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medikoo/1056036 to your computer and use it in GitHub Desktop.
Save medikoo/1056036 to your computer and use it in GitHub Desktop.
Treat low level async functions as promises
var slice = Function.prototype.bind.bind(Array.prototype.slice);
var p = function (fn, args) {
var d = defer();
fn.apply(this, slice(arguments, 1).concat(function (error, result) {
d.resolve((error == null) ? result : error);
}));
return d.promise;
};
p(fs.stat, '/path').then(function (stat) {
// ...
});
p(fs.readFile, '/path', 'utf-8').then(function (text) {
// ...
});
p(path.exists, '/path').then(function (exists) {
// ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment