i have a function which calls the another function and so on.
function A(args,callback){
// make ajax request
// on response
B()
}
function B(args){
// make ajax request
// on response
C()
}
function C(args){
// make ajax request
// on response
D()
}
I am making such ten ajax calls. Two questions...
- can anyone explain me what is callback-hell? Is this a callback Hell?
- If i call callback() inside function D, will it get called. I am not passing callback as argument to my other functions.
-
2I would think really hard before doing 10 ajax calls in a row that all depended on each other, if that's what you're saying.Evan Trimboli– Evan Trimboli2012年11月14日 20:31:42 +00:00Commented Nov 14, 2012 at 20:31
-
2I'm not familiar with a well-established definition for "callback hell", but I would call many deeply-nested callbacks "callback spaghetti".apsillers– apsillers2012年11月14日 20:33:34 +00:00Commented Nov 14, 2012 at 20:33
-
@apsillers. LOL, my thoughts exactly, BTW I've found the definition to that phrase, look below.gdoron– gdoron2012年11月14日 20:34:55 +00:00Commented Nov 14, 2012 at 20:34
-
@EvanTrimboli: Is there any better way to do it?theJava– theJava2012年11月14日 20:35:48 +00:00Commented Nov 14, 2012 at 20:35
3 Answers 3
- I don't know what you're calling callback hell but it's one hell of a spaghetti code.
What is "callback hell"?
Asynchronous javascript, or javascript that uses callbacks, is hard to get right intuitively.
2.No, callback is not defined inside D so you will get an Error.
2 Comments
Inside D() there is no way to call callback because it is not defined there. What I mean by this is as long as you don't pass arguments down the callbacks then you are not having your callback variable inside D(). Callback hell is a situation where callbacks call each-other meaning A() calls B() and B() calls A().
4 Comments
We can pass function reference as a parameter in JavaScript and use this reference to call related function whenever/ wherever we want.
for more info see this link http://recurial.com/programming/understanding-callback-functions-in-javascript/
Comments
Explore related questions
See similar questions with these tags.