Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //customAPI.js
- class EasyHTTP {
- get(url){
- return new Promise((resolve, reject)=> {
- fetch(url, {
- method: 'GET',
- })
- .then(res => res.json())
- .then(data => resolve(data))
- .catch(err => reject(err))
- })
- }
- //post request
- post(url, data) {
- return new Promise((resolve, reject)=> {
- fetch(url, {
- method: 'POST',
- headers:{
- 'content-type': 'application/json'
- },
- body: JSON.stringify(data)
- })
- })
- }
- }
- //app.js
- const http = new EasyHTTP;
- const url = '';
- /*get users*/
- http.get(url)
- .then(data => console.log(data))
- .catch(err => console.log('Something went wrong', err))
- const data = {
- name: 'Jimmy Black',
- username: 'Jeemy',
- email: 'Jimmy@mail.com'
- }
- //send data
- http.post('https://jsonplaceholder.typicode.com/users', data)
- .then(data => console.log(data))
- .catch(err => console.log('Something went wrong', err))
- console.log('Easyhttp')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement