Dva: model的effects里面可以用setTimeout吗

Created on 2 Aug 2017  ·  3Comments  ·  Source: dvajs/dva

代码如下

*aaa({
          payload
      },{call,put}){
        yield put({
            type: 'xxx',
            payload: {...payload}
        });
        setTimeout(
            yield put({
                type: 'xxxx',
            })
        ,1000)
      },

添加setTimeout之后总是报Uncaught SyntaxError: Unexpected identifier,去掉之后就好了,不知道什么原因,是不是在这里面不能用setTimeout?

question

Most helpful comment

试试:

const delay = (ms) => new Promise((resolve) => {
  setTimeout(resolve, ms);
});

*aaa() {
  yield call(delay, 1000);
  yield put...
}

All 3 comments

试试:

const delay = (ms) => new Promise((resolve) => {
  setTimeout(resolve, ms);
});

*aaa() {
  yield call(delay, 1000);
  yield put...
}

@sorrycc 我发现在setTimeout里面使用yeild put并不会起作用,开始报的Uncaught SyntaxError: Unexpected identifier感觉是执行完yeild put之后返回值导致的,如果我想实现发起一个action,这个action首先改变state的值,过几秒后再改变state的某个值怎么办?我需要改变state的某个值来控制某个元素的显隐,由于用的地方特别多,所以我需要在一个action里写所有的动作
自己的理解这个action需要写在effects里面,因为是在这里面才能用call,put,我才能put两个reducer,改变2次state,但是使用yeild貌似不受setTimeout控制,我应该怎样才能定时让一个动作在规定的时间触发(本来是可以在dispatch外面写setTimeout来实现的,但是项目要这么改的话可能好几百个地方都要改,改不过来)

可以花时间理解下 generator 和普通函数的区别,generator 里不能套普通函数。

Was this page helpful?
0 / 5 - 0 ratings