Actionstack Sagas is a middleware package that provides a declarative way to handle asynchronous side effects in Actionstack-powered applications. Using generator functions, it enables complex control flows like sequencing, parallel execution, and error h
npm install @actionstack/sagastypescript
import { createStore } from '@actionstack/store';
import { sagas, run } from '@actionstack/sagas';
import rootSaga from './sagas';
export const store = createStore({
reducer: (state: any = {}) => state,
dependencies: {},
}, applyMiddleware(logger, sagas));
// Run the root saga
store.dispatch(run(rootSaga));
`
$3
Sagas are generator functions that describe side effects in a declarative way. Use effects like take, call, and put to manage actions and state:
`typescript
import { take, call, put } from 'redux-saga/effects';
import { fetchData } from './api';
import { fetchSuccess, fetchFailure } from './actions';
function* fetchDataSaga() {
try {
const data = yield call(fetchData);
yield put(fetchSuccess(data));
} catch (error) {
yield put(fetchFailure(error));
}
}
export default function* rootSaga() {
yield take('FETCH_REQUEST', fetchDataSaga);
}
``