[英]Waiting in a redux-saga
I want to introduce a delay in a saga (using redux-saga).我想在传奇中引入延迟(使用 redux-saga)。
How can I do this?我怎样才能做到这一点?
If redux-saga provides an API, I would also be interested in how to achieve it manually.如果redux-saga提供了一个API,我也想知道如何手动实现。
function* save({ payload }) {
yield put(pending());
// I want to simply wait (non-blocking) here for say 2 seconds
yield put(complete());
}
Redux-sagas has a special effect for this: Redux-saga 对此有特殊效果:
delay(ms, [val])
延迟(毫秒,[val])
Returns a Promise that will resolve after ms milliseconds with val.
返回将在 ms 毫秒后使用 val 解析的 Promise。
Example:例子:
import { delay, call } from 'redux-saga/effects'
function* someSaga(input) {
yield put(someAction())
yield delay(500)
yield put(anotherAction())
}
You could achieve that with a promise and a generator function:你可以通过一个 promise 和一个生成器函数来实现:
function sleep(sec) {
return new Promise(resolve => setTimeout(resolve, sec*1000));
}
function* save({ payload }) {
yield put(pending());
yield sleep(2); //wait 2 seconds
yield put(complete());
}
Using delay as imported from redux-saga/effects would solve your issue in one line使用从 redux-saga/effects 导入的延迟可以一次性解决您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.