Aleksandar
Aleksandar

Reputation: 1776

run after the callback is finished

I need to run a function after the displayPredictionSuggestions callback retrieves and stores its result in a global variable place.

As I understand a callback runs asynchronously. So I can't be sure that the callback is finished when I invoke console.log( JSON.stringfy( place ) );

How do I make sure that the program only continues after the callback is finished (without nesting)?

predictionService = new google.maps.places.AutocompleteService();
predictionService.getPlacePredictions({ 
    input: '1 street, city' }, 
    displayPredictionSuggestions );

displayPredictionSuggestions = function(predictions, status) {

        place = predictions[0]['place_id'];
    }

console.log( JSON.stringfy( place ) );

Upvotes: 0

Views: 43

Answers (1)

kamyl
kamyl

Reputation: 6396

You can avoid nesting callbacks by using Promises.

Upvotes: 1

Related Questions