【问题标题】:Wrap library with thunkify for co用 thunkify for co 包装库
【发布时间】:2023-03-07 23:33:01
【问题描述】:

我想知道如何 thunkify(包装在 thunk 中)一个库以用于 co.该库如下所示。所有客户端调用都使用库中的其他对象。

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;

然后你像这样使用库。

var client = new Library.client(opts);
client.createList();

【问题讨论】:

    标签: javascript node.js generator koa co


    【解决方案1】:

    这里有一个库可以满足您的需求:

    https://www.npmjs.org/package/thunkify-wrap

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 2016-06-08
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      相关资源
      最近更新 更多