bai3/note

高级-同时执行多个任务

Opened this issue · 0 comments

bai3 commented

同时执行多个任务

yield指令可以很简单的将异步控制流以同步的写法表现出来,但与此同时我们将也会需要同时执行多个任务,但是我们不能直接这样写

// effects将按顺序执行
const users = yield call(fetch, '/users'),
	  repos = yield call(fetch, '/repos')

由于第二个effect将会在第一个call执行完毕后才开始。所以我们需要这样写:

import { call } from 'redux-saga/effects'

const [users, repos] = yield [
    call(fetch, '/users'),
    call(fetch. '/repos')
]

当我们需要yield 一个包含effects的数组,Generator会被直到所有的effects都执行完毕,或者当一个effect被拒绝。