I'm using async/await throughout my codebase. Because of this my api calls are defined by async functions
async function apiFetchFoo {
return await apiCall(...);
}
I would like to call this function from my saga code. It seems like I can not do this:
// Doesn't work
function* fetchFoo(action) {
const results = await apiFetchFoo();
yield put({type: "FOOS_FETCHED_SUCCESSFULLY", foos: results});
}
However, this does work, and matches the redux saga documentation:
// Does work
function* fetchFoo(action) {
const results = yield call(apiFetchFoo);
yield put({type: "FOOS_FETCHED_SUCCESSFULLY", foos: results});
}
Is this the correct way to use Redux Saga alongside async/await? It is standard to use this generator syntax inside of the saga code, and the async/await pattern elsewhere?
function *() { ... await }
instead ofasync function () { .. await ...}
? I'm pretty sure if you use await without async it leads to an error "await is a reserved javascript keyword". – Taperasync function* fetchFoo(action: requestAction): AsyncGenerator {
, you need to include"es2018.asynciterable"
to your tsconfig'scompilerOptions.lib
array if you haven't already. Also, I needed TypeScript 3.7 (from 3.0). – Farah