I want to use redux-saga yield call effect with async function that get the result of this function is call back. ( in this case fingerprintjs but it's general case)
This is the async function I want to exactue :
new Fingerprint2().get(function(result, components) {
console.log(result) // a hash, representing your device fingerprint
console.log(components) // an array of FP components
})
The issue that I want to execute this function via saga , but it's always stuck on yield call.
I try many ways :
import Fingerprint2 from 'fingerprintjs2';
const func = new Fingerprint2().get;
const res = yield call(func);
Also try like this:
import Fingerprint2 from 'fingerprintjs2';
const func = new Fingerprint2().get;
const a = yield call(func, (fingerprint, result) => ({
fingerprint,
result
}));
And like this:
import Fingerprint2 from 'fingerprintjs2';
let res = yield call(new Fingerprint2().get(), (fingerprint, result) => ({
fingerprint,
result
}));
Any one know about idea or some way to acheive my goal ?
Thanks!
To anyone want answer to the question , there is serval options:
with cps:
const { result, components } = yield cps(cb => new Fingerprint2().get((result, components) => cb(null, { result, components })))
with promise:
function* rootSaga(){
// Wrapping you function into a promise
const promise = new Promise((resolve, reject) => {
new Fingerprint2().get((result, components) => {
resolve({ result, components })
})
})
// Yield the promise And redux-saga will return the resolved value when the promise resolves
const { result, components } = yield promise
// do some stuff about result and components ...
}
credit: redux-saga github