Last active
March 4, 2016 13:24
-
-
Save dca/9ba39cacc5c14c22baf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
dashboarddistance: function(a, b, c, d, callback) { | |
var ds = 0; | |
var distance = require('google-distance'); | |
distance.get({ | |
index: 1, | |
origin: a + ',' + b, | |
destination: c + ',' + d | |
}, function(err, data) { | |
if (err) { | |
console.log("a"); | |
ds = 0; | |
return callback(err, ds); | |
} else { | |
console.log("b"); | |
ds = data.distance; | |
return callback(null, ds); | |
} | |
}); | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var example = require('./example.js'); | |
example.dashboarddistance(a, b, c, d, function(err, distance) { | |
if (err) { | |
// dosomething and return | |
return ; | |
} | |
console.log(distance); | |
}); |
As You will see in the above code by @dca Hsu it returns distance in variable in distance.
When I use console.log(distance); inside function it gives an output.
But if I want to store this distance in a variable 'finaldistance' and add it to a response what should i do.
I think the scope of distance is only inside callback function that i have passed inside function dashboarddistance.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What I understood is that you want to store the distance returned from the dashboarddistance() in a variable.
But what is the scope of the distance variable, ie where do you want to use it?