126

I've had enough JS for one day.

Comments
  • 7
    Seems about right for jQuery. 😂
  • 1
  • 5
    You may take a look at async.js or Rx.js - nice to flatten out nested callbacks
  • 15
    Use a promise chain instead
  • 7
    I need to see the full code. I'm struggling to believe this is real. No way people code with that much nesting going on, do they?
    (Like x-files, I need proof)
  • 18
    And this is what happens if you don't invest time to learn JS properly
  • 1
    How is this possible?
  • 0
    @yo-adrian - I am not very familiar with JavaScript. Did a bit of Googling on how to make an XHR. I won't be able to show you the code, but here's a brief outline on what I was trying to do. It's API calls to several services that are required to bring up/pull down a node online/offline. A lot of checks have to be made - for example, check the node's load feedback, count peers, calculate weights, check if another node can be brought online when this one goes down, select one, bring it on online, update weights, send the new status to peers, send a test request to see if the new node returns 200, update the new node's load. Just having fun with nested callbacks. I'm sure there are better ways to do this, but I'm quite new to JS (hope to learn it eventually). :)
  • 6
    @clickyotomy use chained promises or transpile async/await or generator functions with co.js

    Callbacks are not a good solution. Promises would solve this issue
  • 3
    @brukernavn32 callback hell!!! 😈
  • 3
    @clickyotomy - if you're using the latest JS release, ES6 new stuff can help you break that out to modules/classes for cleaner code. Of course, would require you include Babel or some transpiler thingie, considering not all browsers support it yet - and it seems MS is not even bothering in IE11, Edge supports it almost fully though. And, you'll feel so much better in the end, I think.

    As others have said, use promises.

    Other things to consider learning: Axios, RxJS.
  • 3
    @clickyotomy Omg bro seriously don't waste your time with libraries or jQuery. Sure libraries can be good, but you will thank us later if you invest the time to learn atleast the fundamentals of javascript first. XHR in pure javascript is so simple.
  • 1
    Real advice: you should put the stuff unto separate functions... It telps with not having a wessy indentation, luke you have ;)
  • 2
    @brukernavn32 what are promises? you just promises to your code your gonna make a function later? haha
  • 3
    @vhoyer it's a standardized api since ES2016. It's like a future in Java. You promise that you'll return a value later in the future (or reject and fail)
  • 1
  • 0
    @brukernavn32 seems nice, better check it, thanks!
  • 0
    Is there some programs that can collapse code between brackets?
  • 1
    @sesar54 webstorm, phpstorm, NetBeans,....
Add Comment