-
-
Save suzdalnitski/91406f2f0ed3550bd9a6b07c66a4af63 to your computer and use it in GitHub Desktop.
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
// 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