【问题标题】:How to yield inside Array.some如何在 Array.some 中产生
【发布时间】:2017-03-18 05:05:43
【问题描述】:

您好,我有一个生成器函数,它返回这样的函数,

*getClassifier(classifier) {
    if (!classifier) {
        return async(function *() {
            return true;
        });
    }

    if (classifier !== null && typeof classifier === "object") {
        return false;
    }

    let name = classifier.name;
    if (!this._classRegistry[name])
        throw new Error("Classifier " + name + " is not registered");
    let com = yield this.injector.resolve(this._classRegistry[name]);

    return (message) => com.classify(message, classifier.options);
}

我为这个函数做了一个单元测试,看起来像这样

result = yield someclass.getClassifiers(classifier)(message);

但后来我对如何使用 chai 获得结果感到困惑 例如expect(result).to.be.true;

任何提示或解决方案将不胜感激。

【问题讨论】:

    标签: node.js asynchronous


    【解决方案1】:

    我不太确定您要在这里完成什么,但是您可以尝试chai-generator 模块,它允许您执行类似(来自文档)的操作:

    expect(generator).to.yield(true);
    

    顺便说一句,我认为您使用了错误的方式,yield 在生成器函数中用于生成值。从外部看,您必须使用迭代器来遍历生成器函数生成的所有值。

    您在第二个代码块中使用 yield 关键字对我来说毫无意义。此外,我不明白您为什么要从生成器返回值。在这种情况下,yield *value* 应该替换 return *value*

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2018-11-01
      • 2017-04-15
      • 2020-07-07
      • 2011-03-23
      相关资源
      最近更新 更多