简体   繁体   English

用thunkify for co包装库

[英]Wrap library with thunkify for co

I was wondering how to thunkify (wrap in thunk) a library for use with co. 我想知道如何对库进行重组(打包)以与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 https://www.npmjs.org/package/thunkify-wrap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM