Created
November 15, 2011 05:01
-
-
Save nanha/1366205 to your computer and use it in GitHub Desktop.
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
| // 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