首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用redux-saga和异步回调函数

如何使用redux-saga和异步回调函数
EN

Stack Overflow用户
提问于 2018-04-16 13:53:40
回答 2查看 1.1K关注 0票数 1

我想用redux-saga,,产率,调用,效应和异步函数,得到这个函数的结果就是回调。(本例中为指纹,但这是一般情况)

这是我想要说明的异步函数:

代码语言:javascript
复制
new Fingerprint2().get(function(result, components) {
console.log(result) // a hash, representing your device fingerprint
console.log(components) // an array of FP components
})

我想通过佐贺来执行这个函数的问题,但它总是停留在屈服调用上。

我尝试了很多种方法:

代码语言:javascript
复制
import Fingerprint2 from 'fingerprintjs2';

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

也可以这样做:

代码语言:javascript
复制
import Fingerprint2 from 'fingerprintjs2';

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

就像这样:

代码语言:javascript
复制
import Fingerprint2 from 'fingerprintjs2';

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

有谁知道我的想法或实现我目标的方法吗?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-28 15:01:48

对于任何想要回答这个问题的人,都有一些选择:

与cps:

代码语言:javascript
复制
const { result, components } = yield cps(cb => new Fingerprint2().get((result, components) => cb(null, { result, components })))

承诺:

代码语言:javascript
复制
 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

票数 0
EN

Stack Overflow用户

发布于 2018-04-17 07:05:06

yield可以接受承诺。

代码语言:javascript
复制
const {result, components} = yield new Promise(resolve => {
  new Fingerprint2().get(function(result, components) {
    resolve({result, components})
  })
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49859027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档