我想使用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,但它们没有区别。)
答案 0 :(得分:0)
您可以尝试蓝鸟。有PromisifyAll选项,它将使您的整个库成为PromisifyAll:
http://bluebirdjs.com/docs/api/promise.promisifyall.html
这是promisifyall mysql库的示例:
dplyr::mutate