【问题标题】:Higland.js: wrap toCallback into promiseHighland.js:将回调包装成承诺
【发布时间】:2017-01-13 16:30:58
【问题描述】:

我想写这样的东西:

const Promise = require('bluebird')
const H = require('highland')

Promise.fromCallback(
  H([1, 2, 3]).toCallback
).then(function(val) {
  expect(val).eql(1, 2, 3)
})

但我看到一个错误:

TypeError: this.consume is not a function

案例中如何正确绑定上下文

【问题讨论】:

    标签: javascript reactive-programming bluebird highland.js


    【解决方案1】:

    第一个问题是 .toCallback 失去了它的竞争,所以 this 不再是一个流,这就是为什么 this.consume 不是一个函数。

    最简单的解决方法是用箭头函数包裹它。

    cb => H([1, 2, 3]).toCallback(cb)
    

    第二件事是你不能使用 toCallback 来发送多个值,因为它会抛出一个错误。 (请查看the docs

    要修复它,您可以像这样调用.collect()

    const Promise = require('bluebird');
    const H = require('highland');
    const assert = require('assert');
    
    Promise.fromCallback(
    	cb => H([1, 2, 3]).collect().toCallback(cb)
    ).then(function(val) {
    	assert.deepEqual(val, [1, 2, 3]);
    	console.log('All good.');
    });

    【讨论】:

      猜你喜欢
      • 2017-07-18
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 2018-05-11
      • 1970-01-01
      • 2015-09-22
      • 2018-05-04
      相关资源
      最近更新 更多