I was wondering how to thunkify (wrap in thunk) a library for use with co. The library is like the following. All the Client calls use other objects in the library.
Library.Client = function(opts) {
}
Library.Client.prototype.createList = function(opts, cb) {
options.client = this;
var list = new Library.List(opts, function(err, data) {
if (typeof(cb) === 'function') {
cb(err, list, data);
}
});
}
Library.List = function(opts, cb) {
// setup with opts
if (cb) this.fetch(cb);
}
Library.List.prototype.fetch = function(cb) {
var list = [1,2];// get list
for (var i = 0; i < list.length; i++) {
list[i] = new Library.Item(list[i]);
});
if (typeof(cb) === 'function') cb(err, data);
}
Library.Item = function(opts) {
}
exports.client = Library.Client;
exports.list = Library.List;
exports.item = Library.Item;
Then you use the library like this.
var client = new Library.client(opts);
client.createList();
here's a library that does what you want:
https://www.npmjs.org/package/thunkify-wrap