Created
December 1, 2017 11:32
-
-
Save Chudesnov/2cf24468e543ad2b643ea9893a93cebc to your computer and use it in GitHub Desktop.
Callbacks
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
// 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