Fwd: withBreak blocks
Jerry Schulteis
jdschulteis at yahoo.com
Sun Feb 18 03:52:10 UTC 2018
No if / else if / else or nested if blocks:
```jsconst log = x => console.log(x);
// Simulated fallible fetchData
const fetchData = () => {
const x = Math.random();
if (x < 0.25) return {error: true, message: "fetchData error"};
if (x < 0.50) return {};
if (x < 0.75) return {data: {}};
return {data: {todos: ["refactor your code"]}};
};
// Simulated action
const action = o => log(o.data);
const checkForError = response => {
const isError = response.error;
if (isError) {
log(response.message);
}
return isError;
};
const checkForNoData = response => {
const isNoData = !response.data;
if (isNoData) {
log("No data");
}
return isNoData;
};
const checkForNoTodos = response => {
const isNoTodos = !response.data.todos;
if (isNoTodos) {
log("No Todos");
}
return isNoTodos;
};
const checkList = [
checkForError,
checkForNoData,
checkForNoTodos
];
const doWork = () => {
const response = fetchData();
const checkFailed = checkList.find(check => check(response));
if (!checkFailed) {
return action({data: response.data});
}
};
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180218/ab0b845f/attachment.html>
More information about the es-discuss
mailing list