Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在effect里setInterval定时修改状态时,yield put不起作用 #1527

Closed
miaojiuchen opened this issue Feb 23, 2018 · 9 comments
Closed

Comments

@miaojiuchen
Copy link

* foo_effect({ payload }, { put }) {
  let interval = setInterval(() => {
    co(function* () {
      console.log('baz')
      yield put({ type: 'foo_reducer' })
    })
  }, 1000)
}

baz能被打印出来,但是foo_reducer没有被执行,求助

@sorrycc
Copy link
Member

sorrycc commented Feb 23, 2018

不能用 setInterval(() => {})

@yvanwangl
Copy link

应该把setInterval(() => {})封装成一个异步函数然后yield吧

@miaojiuchen
Copy link
Author

miaojiuchen commented Feb 23, 2018

大佬能帮忙详细说说吗?
setInterval是可以执行的,是不是yield reducer有啥限制?
@sorrycc

@objectisundefined
Copy link

objectisundefined commented Feb 27, 2018

const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
// or import { delay } from 'redux-saga'

* foo_effect({ payload }, { put }) {
  while (true) {
    console.log('baz')
    yield put({ type: 'foo_reducer' })
    yield delay(1000)
  }
}

这应该能实现你想要的结果,请多查看redux-saga的文档

@miaojiuchen
Copy link
Author

@objectisundefined 有另外的代码会取消的,在这里没写出来 - -

@miaojiuchen
Copy link
Author

miaojiuchen commented Feb 27, 2018

@objectisundefined while(true) 路子有点野 - -,这样后续还想执行别的代码就不行了

@zjxpcyc
Copy link

zjxpcyc commented Mar 21, 2018

@miaojiuchen
while(true) 还好吧,还有大神用 switch(true)

@jnotnull
Copy link

jnotnull/dva-generator#2 (comment) effects中在回调中执行其他effects

@sorrycc sorrycc closed this as completed Apr 18, 2018
@qingjiaowochengzd
Copy link

我往foo_effect的payload中传入参数,根据参数判断是否继续定时请求,这种做法不行,该怎么改?@objectisundefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants