How use a variable outside a callback

David Perez

I have a problem with the persistance of a variable in Node.js.

I make this routine:

var travelTimes;
for(var i = 0; i < data.length; i++){
    distance.get(
    {
        origin: hotel, 
        destination: data[i].ubicacion,
        language: 'es'
    },function(err, maps_data) {
        if (err) return console.log(err);
        travelTimes.push(data.ubication);
    });
}
console.log(travelTimes);

And the last line gives me undefined. I was searching and found it that because Node.js is asynchronous my variable maps_data only lives into the distance.get() callback, but I need that data to continue with my work. How can I make it live in all my code? Thanks!!

baranskistad

How do I use a variable outside of a callback?

You can't.
The variable is defined inside of that callback and that callback only.
This is why it is logging undefined.

You could fix it by putting the console.log() inside of your function.

var travelTimes;
for(var i = 0; i < data.length; i++){
    distance.get(
    {
        origin: hotel, 
        destination: data[i].ubicacion,
        language: 'es'
    },function(err, maps_data) {
        if (err) return console.log(err);
        travelTimes.push(data.ubication);
        console.log(travelTimes); // Now it's inside the callback
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access outside variable in this kind laravel validation callback?

From Dev

How to use a variable outside a PARFOR loop in MATLAB?

From Dev

swift 2 how to use variable outside the function?

From Dev

How to use a variable outside the function in PHP?

From Dev

How to use variable outside the task in gradle script

From Dev

Use a variable outside function

From Dev

A function is outside main,it requires use of a variable how do use it?

From Dev

Javascript, outside variable scope in callback function?

From Dev

How can I use this variable outside Query OnCompleteListener (variable is defined outside OnCreate)?

From Dev

Use value of parameter outside of callback function (undefined)

From Dev

How can I pass the variable in es6 promise then callback to the outside

From Dev

How to use an outside built variable in my function in R

From Dev

How to use variable in a while loop outside of the loop in java?

From Dev

How do i use a variable outside the onResponse in Android?

From Dev

How to use saved variable values outside of gatling scenario in scala file

From Dev

Bash - how to set variable within an if [] condition and use outside

From Dev

how can i use for loop scope variable form outside for loop?

From Dev

How to use an outside built variable in my function in R

From Dev

For loop, how can i use loop variable outside loop

From Dev

Scala : How to use variable in for loop outside loop block

From Dev

how can i use the i variable outside the for loop?

From Dev

How can I use a variable outside of event handler?

From Dev

how to get data from a variable in Def to use it outside and remains updated?

From Dev

How do I use a variable outside of the function it was created in

From Dev

How to use var - variable outside of try-catch block

From Dev

jQuery use variable outside function

From Dev

Use variable outside foreach loop

From Dev

Angular: How to access parameters outside of $timeout callback?

From Dev

How to use $this in outside of class?

Related Related

  1. 1

    How to access outside variable in this kind laravel validation callback?

  2. 2

    How to use a variable outside a PARFOR loop in MATLAB?

  3. 3

    swift 2 how to use variable outside the function?

  4. 4

    How to use a variable outside the function in PHP?

  5. 5

    How to use variable outside the task in gradle script

  6. 6

    Use a variable outside function

  7. 7

    A function is outside main,it requires use of a variable how do use it?

  8. 8

    Javascript, outside variable scope in callback function?

  9. 9

    How can I use this variable outside Query OnCompleteListener (variable is defined outside OnCreate)?

  10. 10

    Use value of parameter outside of callback function (undefined)

  11. 11

    How can I pass the variable in es6 promise then callback to the outside

  12. 12

    How to use an outside built variable in my function in R

  13. 13

    How to use variable in a while loop outside of the loop in java?

  14. 14

    How do i use a variable outside the onResponse in Android?

  15. 15

    How to use saved variable values outside of gatling scenario in scala file

  16. 16

    Bash - how to set variable within an if [] condition and use outside

  17. 17

    how can i use for loop scope variable form outside for loop?

  18. 18

    How to use an outside built variable in my function in R

  19. 19

    For loop, how can i use loop variable outside loop

  20. 20

    Scala : How to use variable in for loop outside loop block

  21. 21

    how can i use the i variable outside the for loop?

  22. 22

    How can I use a variable outside of event handler?

  23. 23

    how to get data from a variable in Def to use it outside and remains updated?

  24. 24

    How do I use a variable outside of the function it was created in

  25. 25

    How to use var - variable outside of try-catch block

  26. 26

    jQuery use variable outside function

  27. 27

    Use variable outside foreach loop

  28. 28

    Angular: How to access parameters outside of $timeout callback?

  29. 29

    How to use $this in outside of class?

HotTag

Archive