Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
You may take a look at async.js or Rx.js - nice to flatten out nested callbacks
-
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) -
@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). :)
-
@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 -
@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. -
yo-yo13638y@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.
-
Real advice: you should put the stuff unto separate functions... It telps with not having a wessy indentation, luke you have ;)
-
vhoyer13538y@brukernavn32 what are promises? you just promises to your code your gonna make a function later? haha
-
@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)
Related Rants
I've had enough JS for one day.
undefined
callbacks
javascript
wtf