lundi 19 décembre 2016

Module pattern + promise use (Returning "undefined"

I used a design pattern (module pattern) to make a function and then invoke a function inside of that which that returns a promise but its returning "undefined"

Why is that? Here is my code:

// spotcrime -----  finds crime loggings of any given area lat/lon + radius
const Crimes = (function () {

    let data;

    let InitCrimes = function (latitude, longitude, radius) {

        let location = {
            latitude,
            longitude
        }

        spotcrime.getCrimes(location, radius, (err, crimeData) => {
            if (err) console.log(err);
            data += crimeData;
        });

        return new Promise ((resolve,reject) => {
            if (data !== undefined || data !== null) {
                resolve(data);
            } else {
                reject(err);
            }
        });
    }

    return {
        el_monte: function (latitude, longitude, radius) {
            InitCrimes(latitude, longitude, radius)
                .then(function(crimeData) {
                return crimeData;
            }).catch(function(error) { return error; });
        }
    }

}());
// 34.052901, -118.019821       // el monte
let y = Crimes.el_monte(34.052901, -118.019821, 0.5);
console.log(y);

Aucun commentaire:

Enregistrer un commentaire