Skip to content

Instantly share code, notes, and snippets.

@suzdalnitski
Last active August 1, 2019 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzdalnitski/91406f2f0ed3550bd9a6b07c66a4af63 to your computer and use it in GitHub Desktop.
Save suzdalnitski/91406f2f0ed3550bd9a6b07c66a4af63 to your computer and use it in GitHub Desktop.
// Wrapping axios to safely call the api without throwing exceptions
const safeApiCall = ({ url, method }) => data =>
axios({ url, method, data })
.then( result => ([null, result]) )
.catch( error => ([error, null]) );
// Partially applying the generic function above to work with the users api
const createUser = safeApiCall({
url: '/api/users',
method: 'post'
});
// Safely calling the api without worrying about exceptions.
const [error, user] = await createUser({
email: 'ilya@suzdalnitski.com',
password: 'Password'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment