Skip to content

Instantly share code, notes, and snippets.

@Chudesnov
Created December 1, 2017 11:32
Show Gist options
  • Save Chudesnov/2cf24468e543ad2b643ea9893a93cebc to your computer and use it in GitHub Desktop.
Save Chudesnov/2cf24468e543ad2b643ea9893a93cebc to your computer and use it in GitHub Desktop.
Callbacks
// const myFetch = require('data-fetching');
function myFetch (url, callback) {
const request = new XMLHttpRequest();
request.onload = function(data) {
callback(null, data);
}
request.onerror = function(error) {
callback(error);
}
request.send();
}
function getProfile(userId) {
myFetch(`${BACKEND_HOST}/api/user/${userId}`, (error, user) => {
const {
firstName,
lastName,
links
} = user;
// const firstName = user.firstName, but with ES2015
// `${first}${last}` === String(first + '' + last)
// links === [ { social_network: 'vk', id: '1' } ]
// const result = {
// fullName: `${firstName} ${lastName}`,
// links: links.map(({ social_network, id }) =>
// getSocialNetworkUrl(social_network, id, url => {
// ...
// })
// }
document.write(user.firstName + ' ' + user.lastName);
});
// get data
// transform data
// show it
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment