产量调用后,Redux saga不会执行

时间:2017-02-27 11:06:29

标签: javascript redux-saga

我有这个deleteSkill处理程序。在这个处理程序中,我正在调用一些api来删除技能。 (yield call(utils.deleteSkill))。它成功地调用了api,但之后没有任何执行。 yield调用后的控制台语句不输出任何内容。我无法搞清楚任何事情。 有人可以帮帮我吗?

import { takeLatest, delay, takeEvery } from 'redux-saga'
import { call, fork, put, race, take } from 'redux-saga/effects'
import * as ActionTypes from './../constants/actions'

/* Handlers */
function* fetchSkills(action) {
 try {
    const { data } = yield call(utils.fetchSkills, action)
    yield put({type: ActionTypes.SUCCESS_FETCH_SKILLS, data})
  } catch (e) {

  }
}

function* deleteSkill(action) {
 try {
    yield call(utils.deleteSkill, action.data) //doesn't execute after this 
    console.log('Hello') //won't execute
    yield put({type: ActionTypes.REQUEST_FETCH_SKILLS})
  } catch (e) {}
}

/* Watchers */
function* watchFetchSkills() {
 while (true) {
    yield* takeLatest(ActionTypes.REQUEST_FETCH_SKILLS, fetchSkills)
  }
}

function* watchDeleteSkill() {
  while (true) {
    yield* takeLatest(ActionTypes.REQUEST_DELETE_SKILL, deleteSkill)
  }
}

export default function* rootSaga() {
 yield [
    fork(watchFetchSkills),
    fork(watchDeleteSkill),
  ]
}

2 个答案:

答案 0 :(得分:1)

我怀疑它是因为在function* watchFetchSkills()function* watchDeleteSkill()中,yield不应该有*。它应该是yield

即使您从 function* watchDeleteSkill()调用另一个生成器函数,它实际上正在调用takeLatest,根据来源:

https://github.com/redux-saga/redux-saga/blob/5fa5d628a80bbb4bfe488288b3ed19b5396f4d14/src/internal/io.js#L164

不是发电机功能。它只是一个简单的旧功能,这意味着不需要yield*

答案 1 :(得分:0)

解构“ action”并将仅必需的参数传递给“ yield call(actionFunc,... args)”可能会有所帮助。