Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active November 27, 2022 20:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WebReflection/569d959caf7128df479974b19bcea526 to your computer and use it in GitHub Desktop.
Save WebReflection/569d959caf7128df479974b19bcea526 to your computer and use it in GitHub Desktop.
const If = expression => {
let call = true, value;
return {
then: callback => Promise.resolve(value).then(callback),
Then(callback) {
if (call && expression) {
call = false;
value = callback(expression);
}
return this;
},
ElseIf(expression) {
return call ? If(expression) : this;
},
Else(callback) {
if (call) {
call = false;
value = callback();
}
return this;
}
};
};
// example
If(condition1)
.Then(value => {
console.log(value);
})
.ElseIf(condition2)
.Then(value => {
console.log(value);
})
.Else(() => {
console.log('no match');
});
@bresnow
Copy link

bresnow commented Nov 27, 2022

I made a react hook similar to this awhile back. running across this is funny. https://github.com/bresnow/utility-react-hooks/blob/master/src/hooks/useIf.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment