Created
September 20, 2019 18:51
-
-
Save nacmartin/520c8f84cc1031e0f75a1586d599d8b1 to your computer and use it in GitHub Desktop.
Sagas workshop: code at the beginning of combinators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as effects from "redux-saga/effects"; | |
import * as constants from "../actions/constants"; | |
function* processTaskTimeConsuming() { | |
yield effects.delay(1000 + 1000 * Math.random()); | |
} | |
function* doProcessTask(name) { | |
try { | |
yield effects.put({ type: constants.TASK_PROCESS_START, name }); | |
yield effects.call(processTaskTimeConsuming); | |
yield effects.put({ type: constants.TASK_PROCESS_DONE, name }); | |
} catch (error) { | |
yield effects.put({ type: constants.TASK_PROCESS_ERROR, name }); | |
} finally { | |
if (yield effects.cancelled()) { | |
yield effects.put({ type: constants.TASK_PROCESS_RESET, name }); | |
} | |
} | |
} | |
function* processTask(action) { | |
yield effects.call(doProcessTask, action.name); | |
} | |
export function* rootSaga() { | |
yield effects.takeEvery(constants.TASK_PROCESS, processTask); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment