Multiple Fault Handling in Angular

Asked

Viewed 55 times

2

I am developing a code and in 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 complete its construction I have to make queries in the database postgres to return the coordinates of all points referring to the area that resulted from the query, my problem is that I have to treat these multiplas promisses only then save the object that was built with those coordinates resulting from the query.

Follows 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.

1 answer

4


You can use the method $q.all 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!');
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.