【问题标题】:Koa and Twitter - "Thunking" does not workKoa 和 Twitter - “Tunking” 不起作用
【发布时间】:2014-07-16 17:11:52
【问题描述】:

我已经通过 thunking 获得了一些第三方异步函数来与 Koa 一起使用,方法是像这样包装函数:var thunkedFunction = function(params) { return function(callback) { originalFunction(params, callback) }; ) 或使用节点 thunkify 库。

但是,当我像这样使用 ntwitter 的流尝试此操作时:

var streamThunk = thunkify(tw.stream);
var stream = yield streamThunk("statuses/filter", {track: track});

我收到以下错误:“无法读取未定义的属性 stream_base”。

深入挖掘 ntwitter(基于 node-twitter)我看到 Twitter.prototype.stream 函数调用 this.options.stream_base,并且 this.options 在我通常调用它时定义,即 tw.stream(function(stream) {...}); 但在我调用时未定义thunk 函数。是否有任何原因导致该函数在重击时失去其范围,有没有办法规避这个问题?

【问题讨论】:

    标签: javascript twitter thunk koa


    【解决方案1】:

    注意thunkify 看不到tw 对象。因此,按照它的设计方式,它无法知道所获取函数的上下文(在您的情况下为 tw)(tw.stream)。

    thunkify 返回的函数将传递调用它的任何 this 上下文 (source: node-thunkify/index.js)。

    这意味着您应该能够将示例中的第二行更改为:

    var stream = yield streamThunk.call(tw, "statuses/filter", {track: track});
    

    Read more about call.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      相关资源
      最近更新 更多