Created
July 5, 2019 09:12
-
-
Save joergpatz/cbb1a756b1d6768b748ccfbc6335e8cc to your computer and use it in GitHub Desktop.
ES6 tagged templates examples
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 math = ([x, y], operation) => | |
operation(Number(x), Number(y)) | |
const plus = (x, y) => x + y | |
const minus = (x, y) => x - y | |
const times = (x, y) => x * y | |
const div = (x, y) => x / y | |
math`3 ${plus} 4` //7 | |
math`3 ${minus} 4` //-1 | |
math`3 ${times} 4` //12 | |
math`3 ${div} 4` //0.75 | |
// LIKE A PROMISE CHAIN WITHOUT then OR catch | |
const go = (_, promise) => (_, yes, no) => | |
go(_, promise.then(yes).catch(no)) | |
const selectName = ({ name }) => { | |
if (!name) throw new Error('Invalid Person. Could not read name.') | |
return name; | |
} | |
go`${fetch('https://swapi.co/api/people/1/?format.json')}` | |
`${res => res.json()}` | |
`${selectName}` | |
`${console.log} ${console.error}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment