admin管理员组文章数量:1317909
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
3 Answers
Reset to default 3Leverage 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.
本文标签: javascriptHow to get fingerprint2 result like functionStack Overflow
版权声明:本文标题:javascript - How to get fingerprint2 result like function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742026746a2415686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论