Skip to content

Instantly share code, notes, and snippets.

@kyleshevlin
Created April 27, 2019 17:38
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 kyleshevlin/58a273884599568260e08acdd38bb520 to your computer and use it in GitHub Desktop.
Save kyleshevlin/58a273884599568260e08acdd38bb520 to your computer and use it in GitHub Desktop.
Partial Application via Bind
// Can't use curried functions for some reason?
// Never fear, partial application via the bind() method is here!
const getFromAPI = (baseURL, endpoint, callback) =>
fetch(`${baseURL}${endpoint}`)
.then(res => res.json())
.then(data => callback(data))
// Partially apply the baseURL
const getFromGithub = getFromAPI.bind(null, 'https://api.github.com')
// Partially apply some endpoints
const getUsersFromGithub = getFromGithub.bind(null, '/users')
const getReposFromGithub = getFromGithub.bind(null, '/repositories')
// Use liberally
getUsersFromGithub(data => {
data.forEach(user => {
console.log(user.login)
})
})
// ["mojombo", "defunkt", "pjhyett", "wycats", "ezmobius"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment