Hacker News new | past | comments | ask | show | jobs | submit login

This is great. Also wanted to toss out its asynchronous nature is not great for beginners either in my opinion.



Why not? What language makes dealing with asynchronous problems easier for a beginner?

    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)


Lol, asynchronous is harder to reason about than synchronous. Plus just about anything goes in the browser. You can put anything anywhere and alot of things are global by default! And as a noob you have enough on your plate learning OOP and REST. I'm glad I started with Ruby and Java and got really good at OOP and clean compartmentalized code before jumping into the madness that is browser land. Just my opinion.


I don't think "is this behavior synchronous or asynchronous" is where we should start when teaching new developers, period.


Passing functions and lambdas and closures is not what newbies are learning...That's like saying: "building bridges isn't hard, you just have to learn kinematics and materials science...But you have to learn that anyway.."




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: