Skip to content

Instantly share code, notes, and snippets.

@lusan
Forked from wayanjimmy/soFetch.js
Created September 7, 2021 12:10
Show Gist options
  • Save lusan/c2ba7f862e2398f9eb5662e70d745221 to your computer and use it in GitHub Desktop.
Save lusan/c2ba7f862e2398f9eb5662e70d745221 to your computer and use it in GitHub Desktop.
Reusable fetch function from wesbos
// https://twitter.com/wesbos/status/1063515277911052290/photo/1
async function soFetch(input, settings = {}) {
const response = await fetch(input, {
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
...settings
});
return response.json();
}
async function useIt() {
// Get Request
const notes = await soFetch(endpoint);
// Post Request
const createdNote = await soFetch(endpoint, {
method: 'POST',
body: JSON.stringify({ title: 'new note' })
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment