最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use yield inside of setTimeOut in redux saga - Stack Overflow

programmeradmin3浏览0评论

In my React project I have the following code:

export function* onDisplayAlert({ payload }: any) {
  payload.id = getUniqueID();
  yield put(setAlert(payload));

  yield setTimeout(() => {
    yield put(removeAlert(payload.id));
  }, 3000);
}

What I want to do here is use yield inside setTimeOut callback.

yield put(removeAlert(payload.id));

But the way I have written this doesn't work, because the arrow function callback is not a generator function, so I can't use yield inside it. How can I use yield inside setTimeOut?

In my React project I have the following code:

export function* onDisplayAlert({ payload }: any) {
  payload.id = getUniqueID();
  yield put(setAlert(payload));

  yield setTimeout(() => {
    yield put(removeAlert(payload.id));
  }, 3000);
}

What I want to do here is use yield inside setTimeOut callback.

yield put(removeAlert(payload.id));

But the way I have written this doesn't work, because the arrow function callback is not a generator function, so I can't use yield inside it. How can I use yield inside setTimeOut?

Share Improve this question edited Jan 11, 2022 at 19:30 TheDataFox 3341 gold badge3 silver badges16 bronze badges asked Jan 17, 2020 at 8:41 Shashika VirajhShashika Virajh 9,46720 gold badges65 silver badges112 bronze badges 3
  • please explain more we did not get your point – Prakash Karena Commented Jan 17, 2020 at 8:46
  • What are you trying to achieve? How do you use onDisplayAlert? – x00 Commented Jan 17, 2020 at 8:47
  • Is the answer of @alex2007v somehow do not suit you? – x00 Commented Jan 17, 2020 at 16:27
Add a ment  | 

1 Answer 1

Reset to default 11

this is what you need

export function* onDisplayAlert({ payload }: any) {
  payload.id = getUniqueID();
  yield put(setAlert(payload));
  yield delay(3000);
  yield put(removeAlert(payload.id));
}
发布评论

评论列表(0)

  1. 暂无评论