Dva: 求教多个异步的请求问题?

Created on 21 Mar 2017  ·  10Comments  ·  Source: dvajs/dva

subscriptions : {
        setup({dispatch, history}) {
            history.listen(location => {
                if (location.pathname === '/xxx') {
                    var id = location.query.id;
                    dispatch({type: 'xx1', payload: {id}});
                    dispatch({type: 'xx2', payload: {id}});
                    dispatch({type: 'xx3', payload: {id}});
                }
            })
        }
    },

因为用了dva-loading, 当xx1请求结束后,loading就为falsel了,其实 xx2 xx3还没有结束。把他们3个放到一起是可以的,但是 这样就会串行了,他们三个其实是并行的。串行耗时太长,求教怎么解决这种情况,谢谢

Most helpful comment

@DrakeXiang 之前是 yield [], 后来 dva 升级了 saga 的版本之后就推荐用 yield all

All 10 comments

在 effect 里写 dispatch 的串行,做流程的控制,不要在 subscription 做异步流程处理。

@zhump 我现在的方案是用eventproxy 或者 async 的 every方法,不知道有没有更好的方法,

@nihgwu 感谢🙏 请接受我的follow

@zhump 也就是说现在有多种方法,一是 yield [] 二是Promise.all([A请求,B请求]) 三是各种奇怪的框架

@sorrycc @ouzhou 求一个串行的demo代码 :pray: :pray:

`
const [xx1_req, xx2_req,xx3_req] = yield [
call(xx1, parse(payload)),
call(xx2, parse(payload)),
call(xx3, parse({reportId})),
]

`
他们确实是同时请求了,,但是 我需要他们谁先回来,谁先执行他们的各自的渲染方法,怎么写?

@nihgwu 怎么effects里面没有这个 all 的方法哇。。

@DrakeXiang 之前是 yield [], 后来 dva 升级了 saga 的版本之后就推荐用 yield all

@DrakeXiang *effects({}, { all }) {}

Was this page helpful?
0 / 5 - 0 ratings