Last active
November 27, 2022 20:30
-
-
Save WebReflection/569d959caf7128df479974b19bcea526 to your computer and use it in GitHub Desktop.
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
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'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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