1

I'm unable to get the values in meals object although i have create new object at the top can any one tell which is the best procedure access variable inside callback function

var meals = new Object();

passObj.data = _.map(passObj.data, (x)=> {               

    x.mealImageUrl = !_.isNull(x.image_url) ? `${config.image_path}${x.image_url}` : x.image;

    dbHelpder.query(`select * from meals where meal_category = ${x.category_id}`,(error,result)=>{

        meals = x.result;

        passObj.total = 555
    });

    return x;
});

2 Answers 2

1

You need to use callback again inside the callback function. :) You are doing something asynchronous, it means, there are no sequence codes. (At least, I keep this in my mind, don't know how others think about this.) So, the code should be:

function somehow(callback) { // you get the result from callback
  var meals = new Object();
  passObj.data = _.map(passObj.data, (x)=> {
    dbHelpder.query(`select * from meals where meal_category = ${x.category_id}`,(error,result)=>{
      meals = x.result;
      passObj.total = 555;
      callback(meals); // Here you get the result
    });
  }
  return x;
}

So, when you are going to use this function, it should be

function afterMeals(resultMeals) {
  // do something on the meals
}
somehow(afterMeals);

Use some other technology can make it a bit clear (like promise), but you can not avoid callback actually.

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

3 Comments

That's how they called it callback hell
Thanks i got it (y)
@MäHį JįlläNį If I solved your problem, please make it as the answer, so I can get a bit reputation. ;) Aha
0

First of all, I cannot see what passObj exactly is, apparently it is defined elsewhere.

Secondly, callback functions don't function the way you seem to think they do. Typically one reason to use them is to implement asynchronous calls, so returning a value is not of use.

The idea is as follows. Usually you have a call like this:

var myFunc1 = function(){

   return 42;

}

var x = myFunc1(); 

myFunc2(x);

However when myFunc1 is an asynchronous call returning a value is impossible without using some sort of promise, which is a topic on its own. So if myFunc1 was an asynchronous call and the 42 was returned e.g. by a server, then just returning a value caused the value to be null, because the return value is not calculated and received yet, when you arrive at return.

This is a reason for callbacks to be introduced. They work in a way, that allows for asynchronous calls and proceeding the way you want to after the call has finished. To show on the example above:

var myFunc1 = function( myFunc2, params ){

   // do async stuff here, then call the callback function from myFunc1
   ...
   myFunc2(x);

}

So the asynchronous function doesn't return anything. It makes the calls or calculations it needs to make and when those are done (in the example that is when x has been declared and assigned a value) myFunc2, which is the callback function in our example, is called directly from the asynchronous function.

Long story short - do what you need to do with x directly inside the callback function.

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.