Skip to content

Instantly share code, notes, and snippets.

@wayanjimmy
Created November 30, 2018 02:41
Show Gist options
  • Save wayanjimmy/45a1ad40f3ae7bb8a2b6716a44093eb1 to your computer and use it in GitHub Desktop.
Save wayanjimmy/45a1ad40f3ae7bb8a2b6716a44093eb1 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