javascript - ElasticSearch JavaScript promise

标签 javascript elasticsearch promise mocha.js

在 Mocha/Chain 启动 block 中测试以下内容,代码永远不会按预期等待。相反,日志报告创建开始,然后记录测试(不包括),然后报告索引创建完成。

Mocha 不应该在 Promise 被解决或拒绝之前退出 before-each block 吗?

我错过了什么?

module.exports.prototype.setup = function (term) {
    this.logger.info("Re-creating the index '%s'", $index);
    return this.client.indices.delete({
        index: $index,
        ignore: [404]
    }).then((err, resp, respcode) => {
        this.logger.info("Creating index '%s'", $index);
        return this.client.indices.create({
            index: $index,
            body: this.schemaBody
        });
    });
};

最佳答案

您需要写一个async test测试 promise 。

it("should do something", (done) => {
    setup("stuff").then((index) => {
        /* test index */
        done();
    });
});

您还可以从测试中返回一个 promise ,mocha 将等待它解决。

it("should do something", () => {
    return setup("stuff").then((index) => {
        /* test index */
    });
});

关于javascript - ElasticSearch JavaScript promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38020261/

相关文章:

javascript - 无法在 module-javascript 外部使用 import 语句

elasticsearch - 查询与过滤器以及查询或过滤器中正确表达式的使用

elasticsearch - 如何使用Elastic Search按格式日期排序?

c++ - 使用 C++ future 作为函数堆栈中的中间值会导致段错误

node.js - 当/Node liftAll 不适用于 s3 对象

javascript - YUI 3.10 简单的滚动动画

javascript - html5 Canvas 。 document.onkeypress 箭头键不起作用

javascript - 如何将现有回调 API 转换为 Promise?

javascript - Math.floor(Math.random() * 255) 不会产生不均匀的概率吗?

elasticsearch - 无法设置index.mapping.single_type:对索引为true