treatment of multiple promisses in angular

2

I'm developing a code and at the same I build some areas of influence for given locations (with coordinates and radius of distance or walking time from the coordinate), so when I finish building it I have to query the database postgres to return the coordinates of all the points referring to the area that was the result of the query, my problem is that I have to treat these multiplas promisses to only then save the object that was constructed with these coordinates resulting from the query. >

Follow the code:

function _checkInfluenceAreas (lat, lon, influenceAreas) {
    for (let influence of influenceAreas) {
        $scope.okToCreate.push({ok: null})
        if (influence.type === 'km') _addInfluenceDist(lat, lon, influenceAreas.indexOf(influence), influence.obj)
        else if (influence.type === 'time') _addInfluenceTime(lat, lon, influenceAreas.indexOf(influence), influence.obj)
        else if (influence.type === 'handdraw') console.log('handdraw')
    }
}


function _addInfluenceDist (lat, lon, index, distObj) {
    let color = distObj.color
    let km = distObj.km * 1000
    let opacity = distObj.opacity
    let promisse = $cartodbService.drawCircleInfluence(lat, lon, km, color, opacity)
    promisse.then(res => {
  // self.locationObj.areasInfluencia[index].obj.theGeom = res.data.rows[0].st_buffer
        $cartodbService.paintCircle(lat, lon, km, color, opacity)
    }).catch(err => {
        console.log(err)
    })
}

function _addInfluenceTime (lat, lon, index, timeObj) {
    let color = timeObj.color
    let time = timeObj.time * 60
    let opacity = timeObj.opacity
    let modo = timeObj.optionTime
    let xyPolyline = []
    let travelInfluence = $cartodbService.drawTravelTimeInfluence(lat, lon, color, time, opacity, modo)
    travelInfluence.then(res => {
  // self.locationObj.areasInfluencia[index].obj.theGeom = res.data.rows[0].the_geom
        let multipolyline = angular.fromJson(res.data.rows[0].st_asgeojson).coordinates
        for (let i = 0; i <= multipolyline[0][0].length - 1; i++) {
            xyPolyline.push([multipolyline[0][0][i][1], multipolyline[0][0][i][0]])
        }
        $cartodbService.paintPolygon(xyPolyline, color, opacity, 10)
    }).catch(err => {
        console.log(err)
})}

Please, if anyone has a suggestion, thank you.

    
asked by anonymous 03.08.2017 / 21:38

1 answer

4

You can use the $q.all method to solve an array of promises:

var promessas = [];

promessas.push(promessa1);
promessas.push(promessa2);
promessas.push(promessa3);

$q.all(promessas).then(function() {
  console.log('resolveu tudo!');
});
    
04.08.2017 / 18:38