如何推广基于原型的类库?

时间:2018-08-08 05:47:35

标签: node.js promise es6-promise

我想使用Promises而不是Node样式的回调来使用GeoPackage库。

使用promisify-node不起作用:

const npromisify = require('promisify-node');
const geopackage = npromisify('@ngageoint/geopackage');

geopackage.openGeoPackage('data.gpkg').then((gpkg) => {
    return npromisify(gpkg.getFeatureTables)();
}).then(tables => {
    console.log(tables);
}).catch(console.error);

不正确地设置了this

TypeError: this.getGeometryColumnsDao is not a function
    at GeoPackage.getFeatureTables (/Users/stevebennett/odev/freelancing/crc-si/ziggurat/node_modules/@ngageoint/geopackage/lib/geoPackage.js:194:18)

way that library function is defined看起来很正常:

GeoPackage.prototype.getFeatureTables = function (callback) {
  var gcd = this.getGeometryColumnsDao();
  gcd.isTableExists(function(err, exists) {
    if (!exists) {
      return callback(null, []);
    }
    gcd.getFeatureTables(callback);
  });
};

该函数中this的值是一个对象,但我无法确定它的确切含义。在任何情况下,函数主体都不是GeoPackage实例。

有没有办法传播这种类型的图书馆?

(我尝试了几种替代方法,例如Node的本机util.promisify和随机Gist,但它们没有区别。)

1 个答案:

答案 0 :(得分:0)

您可以尝试蓝鸟。有PromisifyAll选项,它将使您的整个库成为PromisifyAll:

http://bluebirdjs.com/docs/api/promise.promisifyall.html

这是promisifyall mysql库的示例:

dplyr::mutate