Created
September 20, 2019 16:11
-
-
Save nacmartin/b9853745f463b53cfd31126ed380cd61 to your computer and use it in GitHub Desktop.
Infinite call
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()); | |
if (Math.random() > 0.5) { | |
return true; | |
} else { | |
throw new Error("Something is not right!"); | |
} | |
} | |
export function* doProcessTask(name) { | |
yield effects.put({ type: constants.TASK_PROCESS_START, name }); | |
try { | |
yield effects.call(processTaskTimeConsuming); | |
yield effects.put({ type: constants.TASK_PROCESS_DONE, name }); | |
} catch (error) { | |
yield effects.put({ type: constants.TASK_PROCESS_ERROR, 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