Created
March 21, 2024 11:54
-
-
Save r17x/759aae6d1f8b450a6a90cee52cf416e8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// make it "composable function for ANYTHING" | |
const pipe = (...args) => p => args.reduce((x, f) => f(x), p) | |
// promise RESOLVE handler | |
const pOK = f => p => p.then(f) | |
// promise REJECT handler | |
const pErr = f => p => p.catch(f) | |
// when promise rejected you must be handled the rejection | |
const responsePredicate = f => res => f(res) ? res : promise.reject(res) | |
// I just want 200 and request is OK | |
const responseOK = responsePredicate(res => res.ok && res.status === 200) | |
const responseJSON = res => res.json() | |
const getByUsername = pipe( | |
username => fetch(`https://api.github.com/users/${username}`), | |
pOK(responseOK), | |
pOK(responseJSON), | |
pOK(console.info) | |
// TODO pOK(PUT YOUR FUNCTION HERE), | |
// TODO pErr(WHEN YOU WANT TO HANDLE AN ERROR), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment