Skip to content

Instantly share code, notes, and snippets.

@nanha
Created November 15, 2011 05:01
Show Gist options
  • Select an option

  • Save nanha/1366205 to your computer and use it in GitHub Desktop.

Select an option

Save nanha/1366205 to your computer and use it in GitHub Desktop.
// Takes any async lib that uses callback based signatures and converts
// the specified names to continuable style and returns the new library.
exports.convert = function (lib, names) {
var newlib = {};
Object.keys(lib).forEach(function (key) {
if (names.indexOf(key) < 0) {
return newlib[key] = lib[key];
}
newlib[key] = function () {
var args = Array.prototype.slice.call(arguments);
return function (callback, errback) {
args.push(function (err, val) {
if (err) {
errback(err);
} else {
callback(val);
}
});
lib[key].apply(lib, args)
}
}
});
return newlib;
}
// usage
var fs = Do.convert(require('fs'), ['readdir', 'stat', 'readFile', 'writeFile']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment