Reputation: 19
How can i get out the information from the function? Because when i start the file into the terminal, the
console.log(latitude1 + " " + longitude1);
return me "undefined", instead when i put the
console.log(latitude1 + " " + longitude1);
in the function, returns me the longitue and the latitude.
var triangulate = require('wifi-triangulate')
var latitude1;
var longitude1;
triangulate(function (err, location) {
//if (err) throw err
var latitude = location.lat;
var longitude = location.lng;
latitude1 = latitude;
longitude1 = longitude;
});
console.log(latitude1 + " " + longitude1);
Upvotes: 0
Views: 58
Reputation: 19372
You cannot just run asynchronous (non-blocking) function and after function check for result.
Because JS does not wait (does not block execution) when triangulate
function will finish and go to next line.
For newbies I'll tell that it's something like ajax call in jquery.
Check this example:
var async = require('async');
var triangulate = require('wifi-triangulate');
function doSomethingWithLocation(location, cb) {
console.log(location);
// save to db and etc operations here
cb();
}
async.waterfall([
triangulate,
doSomethingWithLocation
]);
console.log('Here goes triangulate result:'); // while `triangulate` will work, it will output message before that triangulate will pass result to `doSomethingWithLocation`
If You want to continuously get location data and operate on it, checkout this example:
1) We create component called location and put our desired functionality inside of it
components/location.js:
var triangulate = require('wifi-triangulate');
module.exports = function(callback) {
triangulate(function(err, location) {
if(err) {
console.error(err);
return callback({});
}
callback(location);
});
}
2) In app file we require this component and use it and app.js:
var async = require('async');
var getLocation = require('./components/location');
var latitude, longitude;
function handleLocation(lat, lon) {
console.log('Got location information:', lat, lon);
// You can add here db query to update/insert info to db
}
getLocation(function(data) {
latitude = data.lat;
longitude = data.lng;
handleLocation(latitude, longitude);
});
console.log('You cannot see lat, lon', latitude, longitude);
console.log('Because You\'ve to wait for execution of getLocation to complete');
Upvotes: 1
Reputation: 11930
triangulate
package written in callback style, you can convert it to async, or use as it is
var triangulate = require('wifi-triangulate')
function myTriangulate(callback) {
triangulate(function (err, location) {
if (err) throw err;
callback(location);
});
}
myTriangulate(function(loc) {
console.log(loc.latitude + " " + loc.longitude);
});
var triangulate = require('wifi-triangulate')
//use default node's promise lib
//or similar libs like bluebird
function myTriangulate() {
return new Promise(function(resolve, reject) {
triangulate(function (err, location) {
if (err) return reject(err);
resolve(location);
});
});
}
myTriangulate().then(function(loc) {
console.log(loc.latitude + " " + loc.longitude);
});
Upvotes: 1