Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexcastroDev/cbfd438ef79fb708f31e59cc3bbc42df to your computer and use it in GitHub Desktop.
Save AlexcastroDev/cbfd438ef79fb708f31e59cc3bbc42df to your computer and use it in GitHub Desktop.
Closure
function closureRequest() {
let success = false, error = false, data = null;
const dispatch = async (fn) => {
try {
data = await fn();
success = true;
error = false;
} catch {
success = false;
error = true;
}
};
const getSuccess = () => success;
const getError = () => error;
const getData = () => data;
const getDispatch = () => dispatch;
return {
getSuccess,
getError,
getData,
data,
getDispatch,
};
}
const closure = closureRequest()
const { getDispatch, getData } = closure
const dispatch = getDispatch()
await dispatch(async () => {
return await fetch('https://swapi.dev/api/people/1').then(res => res.json())
})
console.log(getData())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment