Skip to content

Instantly share code, notes, and snippets.

@nacmartin
Created September 23, 2019 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nacmartin/51493845c6a7bebaddd3dfb1a74fbaf3 to your computer and use it in GitHub Desktop.
Save nacmartin/51493845c6a7bebaddd3dfb1a74fbaf3 to your computer and use it in GitHub Desktop.
before channels
import * as effects from "redux-saga/effects";
import * as constants from "../actions/constants";
function* processTaskTimeConsuming() {
yield effects.delay(1000 + 1000 * Math.random());
return true;
}
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 });
}
return true;
}
}
function* processTask(action) {
yield effects.call(doProcessTask, action.name);
}
export function* rootSaga() {
while (true) {
const action = yield effects.take(constants.TASK_PROCESS);
yield effects.call(doProcessTask, action.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment