Skip to content

Instantly share code, notes, and snippets.

@droidMakk
Created March 31, 2019 12:12
Show Gist options
  • Save droidMakk/1d76bfd2e2fa68a3ce53249e8da29983 to your computer and use it in GitHub Desktop.
Save droidMakk/1d76bfd2e2fa68a3ce53249e8da29983 to your computer and use it in GitHub Desktop.
Async await, Promises and CallBack
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