Combine elasticsearch js with bluebird

Combine elasticsearch js with bluebird

本文关键字:bluebird with js elasticsearch Combine      更新时间:2023-09-26

我开始与node.js蓝鸟承诺框架。我正试图将其与elasticsearch javascript驱动程序集成。我设法使其工作使用以下代码。我想知道这是不是开始承诺的最好方式。我省略了步骤中调用的函数。

var esPromise = client.search({
    index: "myindex",
    searchType: "count",
    body: {
        aggs: {
            allIp: {
                terms: {
                    field: "ip",
                    size: 1000,
                    order: {
                        _term: "asc"
                    }
                }
            }
        }
    }
});

new Promise(function (resolve, reject) {
    esPromise.then(resolve, reject);
})
    .then(extract_ips_from_aggs)
    .then(sort_ips)
    .then(log_ips)
    .catch(function (error) {
        console.log(error);
    });

您可以覆盖内部使用的延迟对象,如:

var Bluebird = require('bluebird');
var client = new elasticsearch.Client({
  defer: function () {
    // for older Bluebird
    // return Bluebird.defer();
    var resolve, reject;
    var promise = new PromiseImpl(function() {
      resolve = arguments[0];
      reject = arguments[1];
    });
    return {
      resolve: resolve,
      reject: reject,
      promise: promise
    };
  }
});

默认情况下,文档声明它使用ES6 Promise。

阅读更多配置信息