Created
March 31, 2019 12:12
-
-
Save droidMakk/1d76bfd2e2fa68a3ce53249e8da29983 to your computer and use it in GitHub Desktop.
Async await, Promises and CallBack
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
var fetch = require('node-fetch'); | |
const apiUrl = 'https://jsonplaceholder.typicode.com/users'; | |
//Async await method | |
var fetchUsers = async () => { | |
var response = await fetch(apiUrl); | |
var data = response.json(); | |
return data; | |
} | |
var userList = await fetchUsers(); | |
//Promise method | |
var fetchUsersPromise = () => { | |
return new Promise((resolve, reject) => { | |
fetch(apiUrl) | |
.then(response => response.json() | |
.then(data => resolve(data)) | |
.catch(err => reject(err))) | |
.catch(err => reject(err)) | |
}) | |
} | |
fetchUsersPromise() | |
.then(data => console.info('Users',data)) | |
.catch(err => console.error('err',err)); | |
//Callback method | |
var fetchUsersCb = (cb) => { | |
fetch(apiUrl) | |
.then(response => response.json() | |
.then(data => cb(data))) | |
.catch(err => cb(null)) | |
} | |
fetchUserscb(data => console.info('Data',data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment