Skip to content

Instantly share code, notes, and snippets.

@murajun1978
Last active June 5, 2019 17:33
Show Gist options
  • Save murajun1978/46f642c7f0288b93e17e595b5dda3e0f to your computer and use it in GitHub Desktop.
Save murajun1978/46f642c7f0288b93e17e595b5dda3e0f to your computer and use it in GitHub Desktop.
Functional programming in JavaScript
const fetch = require("node-fetch");
export const fetchClient = baseUrl => endpoint => (success, failure) => {
fetch(baseUrl + endpoint)
.then(res => res.json())
.then(success)
.catch(failure);
};
import { fetchClient } from './fetchClient';
const github = fetchClient('https://api.github.com');
export const getGithubUsers = github('/users');
import { fetchClient } from './fetchClient';
const gitlab = fetchClient('https://gitlab.com/api/v4');
export getGitlabUsers = gitlab('/users');
import { getGithubUsers } from './github';
import { getGitlabUsers } from './gitlab';
getGithubUsers(data => console.log('Success!', data), err => console.log('Failure!', err);
getGitlabUsers(data => console.log('Success!', data), err => console.log('Failure!', err);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment