来自redux saga并带来承诺回报

时间:2017-03-12 01:29:17

标签: javascript promise redux generator redux-saga

我无法让yield使用promises代码。当我使用yield函数直接解析返回值时,它一切正常。但是当我使用带有代理函数的yield时,它会返回另一个带有promise的函数 - 它会停止工作。

怎么了?这是示例代码:

const fetchRequest = (payload) => {
    return new API.user().fetch(payload);
};

const fetchRequestFull = (payload) => {
    return new Promise((resolve, reject) => {
        if (payload) resolve({'response'});
    });
};

function* fetchUser(action) {
    try {
        const userData = yield call(new API.user().fetch, action.payload); // doesnt work
        const userDataOther = yield call(fetchRequest, action.payload); // doesnt work
        const userDataYetAnother = yield call(fetchRequestFull, action.payload); // works
        // ...
    } catch (e) {
        yield put({
            type: LOGOUT,
        });
    }
}

0 个答案:

没有答案