javascript - How to make this method asynchronous? -


i writing app uses google javascript api; working splendidly, until want add reverse geo-coding.

the problem have follows: making call method below, geocodelatlng, per record, minimum of 1 record.

i have put traces in, , print out follows:

coordinates: -33.88091325759888, 18.635687828063965

coordinates: -33.874990940093994, 18.639239072799683

coordinates: -33.90454888343811, 18.627684116363525

coordinates: -33.849005699157715, 18.63781213760376

coordinates: -33.85634422302246, 18.639850616455078

then after this, prints out:

returned status is: ok (x5)

i want each call geocodelatlng method completed in entirety, before next 1 attempts start processing. how accomplish this?

function geocodelatlng(callid, lat, lng) {     var returnstring = "";     var latlng = {lat,lng};     console.log("coordinates: " + lat + ", " + lng);     var geocoder = new google.maps.geocoder;      geocoder.geocode({'location': latlng}, function(results, status) {         console.log("returned status is: " + status);         if (status === 'ok') {             if (results[0]) {                 var marker = new google.maps.marker({position: latlng,});                 returnstring =  results[0].formatted_address;                 id_address_map.set(callid, returnstring);              } else {                 returnstring = 'address unknown';                 id_address_map.set(callid, returnstring);             }         } else {             returnstring = 'geocoder failed due to: ' + status;             id_address_map.set(callid, returnstring);         }        });  } 

proposed solution:

function asyncgeocode(callid, lat, lng) {     var returnstring = "";     var latlng = {lat,lng};     console.log("coordinates: " + lat + ", " + lng);     var geocoder = new google.maps.geocoder;        return new promise((resolve, reject) => {           geocoder.geocode({'location': latlng}, function(results, status) {             if (status === "ok") { resolve(results);}             else {reject("some error msg")}         });     }); } 

}

and when called:

for(var = 0; i< markers.length; i++) {     asyncgeocode(markers[i].callid, markers[i].latitude, markers[i].longitude)         .then(             function() {                 console.log("the address known");             },              function(err) {                 console.log("unknown address");             }         ); } 

you wrap promise. like:

function asyncgeocode(callid, lat, lng) {   // ...   return new promise((resolve, reject)) => {     geocoder.geocode({'location': latlng}, function(results, status) {       if (status === "ok") { resolve(results);}       else {reject("some error msg")}     }   })  } 

and use like

asyncgeocode("foo", 1, 2)   .then(resultsformfirscall => asyncgeocode("bar", 123, 321))   ...   .then(() => console.log("all calls done 1 after other")) 

and if can use es7 async/await:

// in async function or async iife  await asyncgeocode("foo", 1, 2); await asyncgeocode("bar", 123, 321); 

in case stuck es5 , can not use async/await or generator functions. like:

function asyncrecursivegeocall(index) {   return asyncgeocode(/*info markers[index]*/)     .then(function() {       if (index < markers.length - 1) {return asyncrecursivegeocall(index + 1)}       else {return promise.resolve()}     }) } asyncrecursivegeocall(0).then(() => console.log("all done")) 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -