Created
October 26, 2022 22:50
-
-
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
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
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