1

I am new to Promise and I have difficulty creating a Promise that will include a Sequelize function which already uses Promise.

I would like to something like this:

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;
            });
        });
    }
}

How can I make the above pointsToGeoJSON function to use Promise, in order to be able to use .then()

Below the Radar
  • 7,321
  • 11
  • 63
  • 142
  • Use bluebird to promisify everything. ```npm install bluebird --save```. I used it in every project. https://github.com/petkaantonov/bluebird – Max Baldwin Jan 26 '16 at 16:30
  • is'nt the same thing than npm promise? – Below the Radar Jan 26 '16 at 16:31
  • Yes and no. Yes, it can create single promises like in your case, but it also has a function called ```promisifyAll``` that can promisify a whole library. It's just a suggestion. That is why I didnt post it as an answer. – Max Baldwin Jan 26 '16 at 16:33
  • 1
    `Geojson.parse` doesn't return a promise, right? So you [cannot `return` from its asynchronous callback](http://stackoverflow.com/q/14220321/1048572), you should [promisify the function](http://stackoverflow.com/q/22519784/1048572) instead. – Bergi Jan 26 '16 at 16:45
  • @MaxBaldwin promisifyAll seems not working with my module, nothing seems to be returned and the resquest is pending indefinately – Below the Radar Jan 26 '16 at 19:35

1 Answers1

2

You already have Promises so just do this: 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);

                    });
            });
        });
    }
};

See the link @bergi provided for an explanation of using Promises with callbacks and some alternatives.

bknights
  • 14,408
  • 2
  • 18
  • 31