I am using Fingerprint2.js (Modern & flexible browser fingerprinting library, a successor to the original fingerprintjs http://valve.github.io/fingerprintjs2/)
I get hash fingerprint when i console.log inside the function. but when i store that result in some variable, it gives 'undifined'
Fingerprint2.get(options, function (components) {
var values = components.map(function (component) { return component.value });
console.log('insideFun -> ' + x64hash128(values.join(''), 31));
return x64hash128(values.join(''), 31);
});
By this, i see a hash code in my console... but if i store the return value to some var it do not work.
if i use asyn/await it still do console value but not store in var
var guid = function guid() {
var fpid;
var options = {}
let fp = (async function() {
const components = await Fingerprint2.getPromise(options);
var values = components.map(function (component) { return component.value });
let fpid = x64hash128(values.join(''), 31);
return fpid;
})();
return fpid;
};
guid();
it give fpid is undefined.
is there any way to handle this?
Fingerprint2.getis an asynchronous function (that's why you need to provide a callback to it); Doingreturn ...inside that callback won't have any effect.You could use
getPromiseinstead, to return a Promise, and use async/await to make your code look like it's synchronous (even though it's not):The problem when you do this:
... is that
console.logis executed before the FingerPrint was retrieved. Because FP2 is asynchronous. So you need to wait for the result before being able to use it.