我希望使用Promises来使用GeoPackage库,而不是Node式的回调。
使用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,但它们没有什么不同。)
发布于 2018-08-08 13:51:18
你可以试试蓝鸟。有一个PromisifyAll的选项,它将简化你的整个库:
http://bluebirdjs.com/docs/api/promise.promisifyall.html
下面是promisifyall mysql库示例:
const connection = mysql.createConnection({.....});
global.db = Bluebird.promisifyAll(connection);
db.queryAsync("SELECT * FROM users").then(function(rows){
console.log(rows);});https://stackoverflow.com/questions/51739384
复制相似问题