javascript - Need help creating async function with callback -


i have json object entries (appointments) each thereof "location id". loop trough these entries , emit request nodejs server socketio data document location id.

at end need array data of lat/lng create marker on map.

here code:

//controller showing map .controller('mapctrl', function($scope, socket){      socket.emit('getapp', staticuserid);          socket.on('getapps', function (appdata) {                 var locarr = [];                 (var = 0; < appdata.length; i++) {                     if (appdata[i].locationid != '') {                         locarr.push(appdata[i].locationid);                     }                 }                 var latlngarr = [];                 (var j = 0; j < locarr.length; j++) {                     socket.emit('getlocation', locarr[j]);                     socket.on('getloc', function (locdata) {                         console.log('received lat/lng: ' + locdata.lat + '/' + locdata.lng);                         if (!latlngarr[j]) latlngarr[j] = []                         latlngarr[j][0] = locdata.lat;                         latlngarr[j][1] = locdata.lng;                     });                 }                 //console.log('test:'+latlngarr[0][0]);         });      var newmarkers = [[52.549678, 13.3879516],[52.5442992, 13.352809],[52.5186283,13.3761181]]; // should generated array     var newcenter = [52.549678, 13.3879516];     createmap(newcenter,newmarkers);  }) 

the problem is, var latlngarr isn't defined out of the...

socket.on('getloc', function (locdata) 

it nice if can me :-)

thanks much!

if can use promises

.controller('mapctrl', function($scope, socket){      socket.emit('getapp', staticuserid);      socket.on('getapps', function (appdata) {         var locarr = [];         (var = 0; < appdata.length; i++) {             if (appdata[i].locationid != '') {                 locarr.push(appdata[i].locationid);             }         }         var latlngarr = [];         var promises = [];         (var j = 0; j < locarr.length; j++) {             promises[j] = (function(captured_j) {                 return new promise(function(resolve, reject) {                     socket.emit('getlocation', locarr[captured_j]);                     socket.on('getloc', function (locdata) {                         console.log('received lat/lng: ' + locdata.lat + '/' + locdata.lng);                         if (!latlngarr[captured_j]) latlngarr[captured_j] = []                         latlngarr[captured_j][0] = locdata.lat;                         latlngarr[captured_j][1] = locdata.lng;                         resolve({index: captured_j, result: latlngarr[captured_j]});                     });                 });             }(j));         }         promise.all(promises).then(function(arr) {             // ******************************************             // ******************************************             // arr array of {index: #, result [lat, lng]} - can use latlngarr             // ******************************************             // ******************************************         });     });     var newmarkers = [[52.549678, 13.3879516],[52.5442992, 13.352809],[52.5186283,13.3761181]]; // should generated array     var newcenter = [52.549678, 13.3879516];     createmap(newcenter,newmarkers); }) 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -