gpt4 book ai didi

javascript - 高地.js : wrap toCallback into promise

转载 作者:太空宇宙 更新时间:2023-11-04 16:09:19 25 4
gpt4 key购买 nike

我想写这样的东西:

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

如何正确绑定(bind)案例中的上下文

最佳答案

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

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

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

第二件事是,您不能将 toCallback 与发出多个值的 steam 一起使用,因为它会抛出错误。 (请查看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.');
});

关于javascript - 高地.js : wrap toCallback into promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41639259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com