Skip to content

Instantly share code, notes, and snippets.

@dorianbayart
Created October 26, 2022 22:50
Show Gist options
  • Save dorianbayart/154af8f0b2aa8999c8abf2b609404f02 to your computer and use it in GitHub Desktop.
Save dorianbayart/154af8f0b2aa8999c8abf2b609404f02 to your computer and use it in GitHub Desktop.
Utils - An async function to Get or Post on url depending on a query parameter
async function get(url, query = null) {
if(query) { // if query => /post
return new Promise((resolve, reject) => {
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query })
})
.then((response) => response.json())
.then(resolve)
.catch(reject)
})
}
// default is /get
return new Promise((resolve, reject) => {
fetch(url)
.then((response) => response.json())
.then(resolve)
.catch(reject)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment