我无法让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,
});
}
}