const getTweets = (username) => return fetch(`https://api.twitter.com/${username}`)
getTweets('nevon').then(console.log).catch(console.error)
Really isn't that bad, although you have to know promises. With regular callbacks, I guess you have to know that functions can be passed as arguments, but you'll have to learn that quite soon regardless. const getTweets = (username, callback) => {
const req = new XMLHttpRequest()
req.onload = function () {
return request.status === 200 ? callback(undefined, JSON.parse(this.responseBody)) : callback(Error(request.statusText))
}
req.onerror = callback
req.open('get', `https://api.twitter.com/${username}`, true)
req.send()
}
getTweets('nevon', (err, tweets) => {
if (err) {
console.error(err)
} else {
console.log(tweets)
}
})
Most of the confusing crap in there is because of XMLHttpRequest, not asynchronicity.With async functions, which I personally am not a huge fan of, you can get synchronous looking code if you believe that's more beginner friendly:
async function getTweets (username) => {
const response = await fetch(url)
return response.json()
}
getTweets('nevon').then(console.log).catch(console.error)