最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get fingerprint2 result like function - Stack Overflow

programmeradmin1浏览0评论

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

Fingerprint2 is a Modern & flexible browser fingerprinting library / Usage:

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

whatever try i did to get result of Fingerprint2 outside of new Fingerprint2().get(function(result, ponents){ 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();

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, ponents){
  console.log(result); //a hash, representing your device fingerprint
  console.log(ponents); // an array of FP ponents
});

whatever try i did to get result of Fingerprint2 outside of new Fingerprint2().get(function(result, ponents){ 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();
Share Improve this question asked Oct 8, 2016 at 13:06 محمد علی پور فتحکوهیمحمد علی پور فتحکوهی 4135 silver badges20 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

See get and getPromise in Fingerprint2 Doc

This should be a ment 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.

You can't make async code act pletely 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, ponents) => 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, ponents) => 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.

发布评论

评论列表(0)

  1. 暂无评论