Skip to content

Instantly share code, notes, and snippets.

@datvtwkm
Last active August 26, 2018 07:42
Show Gist options
  • Save datvtwkm/dd8c9c46bd47c179c932bc8eabfaba1f to your computer and use it in GitHub Desktop.
Save datvtwkm/dd8c9c46bd47c179c932bc8eabfaba1f to your computer and use it in GitHub Desktop.
import {
call,
put,
take,
takeLatest,
race,
select
} from 'redux-saga/effects';
import {
getContainer,
removeContainer
} from "../utils/global-container";
import api from './api';
import actions from '../actions'
const {
start, cancel, succeed, fail
} = actions;
function* fetchSomeThing() {
const container = getContainer('callingContainer');
if (container && container.name === 'callingContainer') {
container.setState(()=>({
loading: true,
result: ''
}))
}
try {
yield call(api);
yield put(succeed());
if (container) {
container.setState({
loading: false,
result: 'success'
})
}
} catch (error) {
yield put(fail());
if (container) {
container.setState({
loading: false,
result: 'fail',
error
})
}
} finally {
removeContainer('callingContainer')
}
}
const fetchSomeThingSaga = function* () {
yield takeLatest([start], function* (...args) {
yield race({
task: call(fetchSomeThing, ...args),
cancel: take(cancel)
});
});
};
export default fetchSomeThingSaga;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment