DEV Community

[Comment from a deleted post]
Collapse
 
vonheikemen profile image
Heiker

Some of the benefits that you mention I think are more related with pure functions than currying itself. Though falsey values like null and undefined can still be a problem, if you pass undefined to getApi you probably won't get what you want.

I think currying is really helpful when you make heavy use of higher order functions. Using your example, you could take it even further with a couple of helper functions.

var map = cb => arr => arr.map(cb);
var get = prop => obj => obj[prop];

var showNames = map(compose(console.log, get('name')));

starWarsCharacters(compose(showNames, get('results')));

When writting imperative code currying is rarely used or needed. But the good thing about javascript is that you don't have to curry functions in order to take advantage of partial application, Function.bind can be quite helpful sometimes. You could write getApi like you would a normal function and still be able to create those specialized functions.

function getApi(baseUrl, endpoint, cb) {
    return fetch(`${baseUrl}${endpoint}`)
    .then(res => res.json())
    .then(data => cb(data))
    .catch(err => console.log('Error:', err)); 
}

var starWarsBaseUrl = "https://swapi.co/api/";
var getFromStarWars = getApi.bind(null, starWarsBaseUrl);
var starWarsCharacters = getFromStarWars.bind(null, 'people');

starWarsCharacters(data => data.results.map(person => console.log(person.name)));
 
nhidtran profile image
nhidtran

Thanks for sharing that! I've never used Function.bind and you've provided a really awesome use case for it. And I think I'm going to re-use that implementation for the curried function in another project.