【问题标题】:redux-thunk repeat redux-api-middlware action until timeout or state variable setredux-thunk 重复 redux-api-middleware 操作,直到超时或设置状态变量
【发布时间】:2019-07-29 01:53:50
【问题描述】:

我有一个端点,可以通过 GET 请求进行轮询,如果任务完成,它就会返回。
{"status":"running"}

{"status":"completed"}
我正在使用 redux-api-middleware 创建进行 api 调用的操作。

export const TASK_REQUEST = '@@unshorten/TASK_REQUEST';
export const TASK_SUCCESS = '@@unshorten/TASK_SUCCESS';
export const TASK_FAILURE = '@@unshorten/TASK_FAILURE';


export const check_task = (hash) => ({
 [RSAA]: {
    endpoint: '/api/task_status/'+hash+'/',
      method: 'GET',
      headers: withAuth({ 'Content-Type': 'application/json' }), 
      types: [
        {   
          type:RASTERIZE_REQUEST, 
          meta:{hash:hash}
        },  
        {   
          type:RASTERIZE_SUCCESS, 
          meta:{hash:hash}
        },  
        {   
          type:RASTERIZE_FAILURE, 
          meta:{hash:hash}
        },  
      ]   
  }

})

我需要继续调用此操作并在两次调用之间等待 20 秒,直到端点返回 {"status":"completed"} 或达到 120 秒的最大超时时间。

我建议的解决方案是使用带有异步等待 20 秒的 redux thunk,重复调用上述操作,直到返回状态完成或达到超时。

上述部分我需要这样的商店:

{
initial_call_time:1,
last_call_time:12,
status:'running'
}

这样我可以在接下来的几次调用中检查存储变量,看看时间是否已过期或状态是否已完成

有更好的解决方案来处理这个用例吗?

【问题讨论】:

    标签: reactjs redux-thunk


    【解决方案1】:

    Redux Saga 是我发现的最好的投票框架。这是一个很好的教程,准确描述了我要解决的情况。

    https://levelup.gitconnected.com/keep-calm-and-race-on-redux-saga-case-study-2f5b0a6630ae

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-04
      • 2018-03-20
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 2016-11-12
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多