0

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...

  1. can anyone explain me what is callback-hell? Is this a callback Hell?
  2. If i call callback() inside function D, will it get called. I am not passing callback as argument to my other functions.
4
  • 2
    I 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. Commented Nov 14, 2012 at 20:31
  • 2
    I'm not familiar with a well-established definition for "callback hell", but I would call many deeply-nested callbacks "callback spaghetti". Commented Nov 14, 2012 at 20:33
  • @apsillers. LOL, my thoughts exactly, BTW I've found the definition to that phrase, look below. Commented Nov 14, 2012 at 20:34
  • @EvanTrimboli: Is there any better way to do it? Commented Nov 14, 2012 at 20:35

3 Answers 3

3
  1. 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.

Source

2.No, callback is not defined inside D so you will get an Error.

Sign up to request clarification or add additional context in comments.

2 Comments

Lets take if i pass my callback reference from A to B and C then receive it in D... will i able to achieve.
@theJava, Yes, in that case it will be valid, probably spaghetti but valid. I've never seen a good reason for doing 10 async operations one after the other. usually when those kind of things are being done, it's a mistake.
1

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

Imho you don't need (endless) "recursion" for a callback hell.
@Bergi You are right, but callbacks are not that hard to debug, but the real hell is when callbacks are constantly calling each-other, because then debugging results in a different output than what you would get if you do not breakpoint inside the callbacks :)
If i pass my callback reference via A to B and B to C an then C to D... will that result in callback hell
@theJava This begs to the question is dynamic programming a callback hell? In your case I would not call it a callback hell.
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.