Created
April 27, 2019 17:38
-
-
Save kyleshevlin/58a273884599568260e08acdd38bb520 to your computer and use it in GitHub Desktop.
Partial Application via Bind
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
// 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