Last active
June 23, 2019 11:17
-
-
Save Siemko/b944e3bd29e16494970b459e85ae640d to your computer and use it in GitHub Desktop.
[async fetch api request] #js #javascript #http
This file contains hidden or 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
export const request = async (url, method = 'GET', type = 'json', body = null) => { | |
const requestOptions = { | |
method: method, | |
headers: { 'Content-Type': 'application/json' }, | |
}; | |
if (body) { | |
requestOptions.body = JSON.stringify(body); | |
} | |
if (type === 'json') { | |
return await (await fetch(url, requestOptions)).json(); | |
} else { | |
return await (await fetch(url, requestOptions)).status; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment