Skip to content

Instantly share code, notes, and snippets.

@r17x
Created March 21, 2024 11:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r17x/759aae6d1f8b450a6a90cee52cf416e8 to your computer and use it in GitHub Desktop.
Save r17x/759aae6d1f8b450a6a90cee52cf416e8 to your computer and use it in GitHub Desktop.
// 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