控制台中的Javascript生成器语法错误

时间:2016-08-11 14:04:45

标签: javascript webpack babeljs redux-saga

使用发电机时遇到问题。我在控制台中收到以下错误:

   ./app/redux/sagas/tracking.saga.js中的错误模块构建失败:   语法错误:   C:/Workspace/teamable-frontend/app/redux/sagas/tracking.saga.js:   意外的令牌(18:4)

以下是package.json

{
 "devDependencies": {
    "autoprefixer-loader": "^3.2.0",
    "babel-cli": "^6.4.5",
    "babel-core": "^6.4.5",
    "babel-loader": "^6.2.1",
    "babel-plugin-transform-runtime": "^6.12.0",
    "babel-polyfill": "^6.9.1",
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-react": "^6.3.13",
    "babel-preset-stage-0": "^6.5.0",
    "babel-runtime": "^6.11.6",
    ...
    }
...
}

webpack.config中的加载程序:

module: {
    loaders: [{
        test: /.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
            query: {
            presets: ['es2015', 'react', 'stage-0'],
            plugins: ["transform-runtime"]
            }
        },
   ...
}

使用生成器的函数:

import {put, call} from 'redux-saga/effects';
import {takeEvery} from 'redux-saga';

import {LOAD} from '../../constants/ActionTypes';
import {loadTrackingItemsSuccess, loadTrackingItemsFail} from '../actions/tracking.actions';
import {getTrackingItems} from '../../mocks/ListMock'

function* loadTrackingItems() {
    try {
        const trackingItems = yield call(getTrackingItems);
        yield put(loadTrackingItemsSuccess(trackingItems));
    } catch(ex) {
        yield put(loadTrackingItemsFail(ex.toString()));
    }
}

export function watchTrackingItemsLoad() {
    yield* takeEvery(LOAD, loadTrackingItems);
}

我做错了什么?

1 个答案:

答案 0 :(得分:4)

yieldyield*只能在生成器函数中使用。这里:

export function watchTrackingItemsLoad() {
    yield* takeEvery(LOAD, loadTrackingItems);
}

您在正常功能中使用yield*。该函数也应该是生成器(function* watchTrackingItemsLoad),或者您应该返回生成器对象并让调用者处理它(return takeEvery(LOAD, loadTrackingItems);)。