Created
November 30, 2018 02:41
-
-
Save wayanjimmy/45a1ad40f3ae7bb8a2b6716a44093eb1 to your computer and use it in GitHub Desktop.
Reusable fetch function from wesbos
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
// 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