5

I want to make a function that get the reslt of fingerprint2.js

Fingerprint2 is a Modern & flexible browser fingerprinting library http://valve.github.io/fingerprintjs2/ Usage:

new Fingerprint2().get(function(result, components){
  console.log(result); //a hash, representing your device fingerprint
  console.log(components); // an array of FP components
});

whatever try i did to get result of Fingerprint2 outside of new Fingerprint2().get(function(result, components){ was failed. like Global vars and cookie because Fingerprint2().get(...) is asynchronous Can it be written like a function to get fingerprint2 result? for example:

var secure = getmefingerprint2();

3 Answers3

3

Leverage with ES2017 feature async/await, you can use Fingerprint2.getPromise() like this:

(async () => {
    const components = await Fingerprint2.getPromise();
    const values = components.map(component => component.value);
    const murmur = Fingerprint2.x64hash128(values.join(""), 31);
    console.log('fingerprint:', murmur);
)()

See get and getPromise in Fingerprint2 Doc

陈柏林
  • 74
  • 4
1

This should be a comment but its a bit long.

Even if it were possible, you would be bypassing the published api, meaning you would have to maintain a fork of the original code. You would also need to invoke the functionality synchronously - and fingerprintjs2 runs asynchronously for good and obvious reasons.

You seem to be asking about an XY problem

How you should sole it depends on what you intend to do with the fingerprint after it has been captured.

Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94
0

You can't make async code act completely synchronous. However, you can use async/await, if your target browser has support, but it's not universally supported. Also, it only looks synchronous inside the async function

The basic idea is to return a promise, then await it inside an async function:

const getmefingerprint2 = async () => {
  const secure = await (new Promise(resolve => {
    new Fingerprint2().get((result, components) => resolve(result) )
  }))
  // do things with secure, whatever you return is thenable
  return secure
}

that function could be called like this (because of Promises):

getmefingerprint2().then(result => {
  // do stuff with result
})

but also, inside the async function, you could treat secure like you got it synchronously.

If you really wanted to make your async code act more sync (might be useful for other async code, too, if you hate async), you could wrap all your code in an async function, then use await to get async stuff:

const getFingerprint = () => new Promise(resolve => {
  new Fingerprint2().get((result, components) => resolve(result) )
})

const main = async () => {
  // do some of your app here
  const secure = await getFingerprint()
  // do more stuff here
}

main()

Or as an IIFE:

(async() => {
  // do some of your app here
  const secure = await getFingerprint()
  // do more stuff here
})()

These are just kinda hacky workarounds that allow you to escape the burden of async code, which is maybe worth just getting to know, as it will make a better app. If you restructure your code to only have the things that depend on secure inside the callback, you'll get better performance, unblocked UI, and a more dynamic flow that is easy enough to reason about, once you get used to it.

konsumer
  • 3,411
  • 1
  • 30
  • 31
  • Also, you can use polyfills + babel to make it work in older browsers. [Here](http://codepen.io/konsumer/pen/QpXvOr?editors=0010) is an example of what you are trying to accomplish in codepen. – konsumer Apr 07 '17 at 18:39