1

我想使用带有异步函数的 redux-saga产生调用效果,该函数的结果是回调。(在这种情况下是指纹,但这是一般情况)

这是我想要精确的异步功能:

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

我想通过 saga 执行这个函数的问题,但它总是卡在 yield 调用上。

我尝试了很多方法:

import Fingerprint2 from 'fingerprintjs2';

const func = new Fingerprint2().get;
const res = yield call(func);

也试试这样:

import Fingerprint2 from 'fingerprintjs2';

const func = new Fingerprint2().get;
const a = yield call(func, (fingerprint, result) => ({
  fingerprint,
  result
}));

像这样:

import Fingerprint2 from 'fingerprintjs2';

let res = yield call(new Fingerprint2().get(), (fingerprint, result) => ({
  fingerprint,
  result
}));

任何人都知道想法或某种方式来实现我的目标?

谢谢!

4

2 回答 2

1

yield可以接受 Promise。

const {result, components} = yield new Promise(resolve => {
  new Fingerprint2().get(function(result, components) {
    resolve({result, components})
  })
})
于 2018-04-17T07:05:06.927 回答
0

对于任何想回答这个问题的人,有多种选择:

与cps:

const { result, components } = yield cps(cb => new Fingerprint2().get((result, components) => cb(null, { result, components })))

承诺:

 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 ...
}

信用:redux-saga github

于 2018-10-28T15:01:48.267 回答