如何在React Redux-Saga中设置yield调用的超时

Tal*_*lik 7 reactjs redux-saga react-redux

在我的 REST API 后端中,我进行繁重的处理,通常需要 1.5 分钟才能生成结果,此时我在前端 React 应用程序中收到此错误。

Error: timeout of 60000ms exceeded
Run Code Online (Sandbox Code Playgroud)

因此,对等连接丢失。

如何在 redux-saga 中设置请求超时

小智 10

我被用来race做这样的事情。也许它对你有用。

  const {posts, timeout} = yield race({
    posts: call(fetchApi, '/posts'),
    timeout: delay(60 * 1000)
  });
  if (timeout) throw new Error('timeout of 60000ms exceeded') 
Run Code Online (Sandbox Code Playgroud)


小智 1

import { eventChannel, END } from 'redux-saga'

function countdown(secs) {
  return eventChannel(emitter => {
      const iv = setInterval(() => {
        secs -= 1
        if (secs > 0) {
          emitter(secs)
        } else {
          // this causes the channel to close
          emitter(END)
        }
      }, 1000);
      // The subscriber must return an unsubscribe function
      return () => {
        clearInterval(iv)
      }
    }
  )
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。