NodeJS - 创建一个使用 Sequelize 函数的 Promise 函数
NodeJS - Create a Promise function that use Sequelize function
我是 Promise 的新手,我很难创建一个包含已使用 Promise 的 Sequelize 函数的 Promise。
我想要这样的东西:
var geojson = require(path.join(__dirname, 'lib', 'geojson');
router.get('/[0-9]{3}/points.geojson', function(req, res, next){
var codecs = req.url.split('/')[1];
geojson.pointsToGeoJSON(codecs).then(function(geoJSON){
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(geoJSON));
});
});
./lib/geojson.js:
var path = require('path');
var Promise = require('promise');
var Geojson = require('geojson');
var Point = require(path.join(__dirname, '..', '..', 'models', 'point'));
module.exports = {
pointsToGeoJSON: function(codecs) {
//this is a Sequelize function
Point.findAll({where: {codecs : codecs}, raw: true})
.then(function(points){
var data = [];
for(var i=0; i < points.length; i++){
points[i].coordinates = JSON.parse(points[i].coordinates);
data.push(points[i]);
}
//this is another asyn function
Geojson.parse(data, {'crs': {'type': 'name', 'properties': {'name': 'EPSG:3857'}}, 'Point': 'coordinates'}, function(geoJSON){
//this is what I want the function to return
return geoJSON;
});
});
}
}
如何让上面的pointsToGeoJSON
函数使用Promise,才能使用.then()
您已经有了 Promise,所以只需执行以下操作:
var path = require('path');
var Promise = require('promise');
var Geojson = require('geojson');
var Point = require(path.join(__dirname, '..', '..', 'models', 'point'));
module.exports = {
pointsToGeoJSON: function(codecs) {
//this is a Sequelize function
return Point.findAll({where: {codecs : codecs}, raw: true}).then(function(points){
var data = [];
for(var i=0; i < points.length; i++){
points[i].coordinates = JSON.parse(points[i].coordinates);
data.push(points[i]);
}
//this is another asyn function
return new Promise(function(resolve, reject){
Geojson.parse(
data,
{'crs': {'type': 'name', 'properties': {'name': 'EPSG:3857'}}, 'Point': 'coordinates'},
function(geoJSON){
resolve(geoJSON);
});
});
});
}
};
请参阅 link @bergi 提供的有关将 Promises 与回调和一些替代方法结合使用的说明。
我是 Promise 的新手,我很难创建一个包含已使用 Promise 的 Sequelize 函数的 Promise。
我想要这样的东西:
var geojson = require(path.join(__dirname, 'lib', 'geojson');
router.get('/[0-9]{3}/points.geojson', function(req, res, next){
var codecs = req.url.split('/')[1];
geojson.pointsToGeoJSON(codecs).then(function(geoJSON){
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(geoJSON));
});
});
./lib/geojson.js:
var path = require('path');
var Promise = require('promise');
var Geojson = require('geojson');
var Point = require(path.join(__dirname, '..', '..', 'models', 'point'));
module.exports = {
pointsToGeoJSON: function(codecs) {
//this is a Sequelize function
Point.findAll({where: {codecs : codecs}, raw: true})
.then(function(points){
var data = [];
for(var i=0; i < points.length; i++){
points[i].coordinates = JSON.parse(points[i].coordinates);
data.push(points[i]);
}
//this is another asyn function
Geojson.parse(data, {'crs': {'type': 'name', 'properties': {'name': 'EPSG:3857'}}, 'Point': 'coordinates'}, function(geoJSON){
//this is what I want the function to return
return geoJSON;
});
});
}
}
如何让上面的pointsToGeoJSON
函数使用Promise,才能使用.then()
您已经有了 Promise,所以只需执行以下操作: var path = require('path'); var Promise = require('promise'); var Geojson = require('geojson'); var Point = require(path.join(__dirname, '..', '..', 'models', 'point'));
module.exports = {
pointsToGeoJSON: function(codecs) {
//this is a Sequelize function
return Point.findAll({where: {codecs : codecs}, raw: true}).then(function(points){
var data = [];
for(var i=0; i < points.length; i++){
points[i].coordinates = JSON.parse(points[i].coordinates);
data.push(points[i]);
}
//this is another asyn function
return new Promise(function(resolve, reject){
Geojson.parse(
data,
{'crs': {'type': 'name', 'properties': {'name': 'EPSG:3857'}}, 'Point': 'coordinates'},
function(geoJSON){
resolve(geoJSON);
});
});
});
}
};
请参阅 link @bergi 提供的有关将 Promises 与回调和一些替代方法结合使用的说明。