0

How to retrieve value from callback ? I need first to find student by name and then calculate something and back result. Function which finds student by name returns err and result in callback like

function findStudentAndCalculate(name, cb);

cb is callback which takes as parameters err and student instance. I passed for cb

function calculateSomething(err, st){
    if(err) throw new Error("Error")
    var result = some stuff with st;
    return result;
}

I should have this return to response to page for given url with name parameter

findStudentAndCalculate("John", calculateSomething);
PaolaJ.
  • 10,872
  • 22
  • 73
  • 111

2 Answers2

0

Well, you haven't provided us enough information for findStudentAndCalculate to know what values for use for the callback parameters, or how you want to store or return the return value from the callback, but . . . in general . . . when you pass in a function as a parameter, you just treat the function as if it had been defined using the parameter as the function name. So, in your case, at some point in findStudentAndCalculate, you would make a call like this:

var someVariable = cb(errValue, stValue);

. . . or . . .

return cb(errValue, stValue);

The code will then act as if you had directly called:

var someVariable = calculateSomething(errValue, stValue);

. . . or . . .

return calculateSomething(errValue, stValue);

(respectively)

talemyn
  • 7,822
  • 4
  • 31
  • 52
  • This would assume that `findStudentAndCalculate()` is synchronous. While it's possible, it would make one wonder why it receives a callback. – cookie monster Feb 04 '14 at 00:23
  • @cookiemonster - I can think of reasons . . . if there are a whole bunch of functions that rely on student information, someone might set up a function that retrieves the student information in a standard way and then passes it on to the next function (the name actually hints at that). I'm not saying it's the optimal approach, but, after many years of working on other peoples code, I've learned not to get too hung-up on why people have chosen one approach over another. :) – talemyn Feb 04 '14 at 17:21
0

This callback pattern is most frequently used with asynchronous code. Given that you tagged your post "nodejs" and "promise", I'm going to assume that findStudentAndCalculate is asynchronous (e.g. it has to look up the student in a database somewhere, which takes time) and so you won't be able to do anything with it's return value directly.

There are two possibilities here:

  1. If findStudentAndCalculate is a typical Node.js-style async function (and it sure looks like a typical function), then it won't return anything useful. Instead, you'll need to do all of your work inside the callback function that you provided (that is, inside, calculateSomething itself). In other words, you'll need to not just calculateSmething but calculateSomethingAndThenDoSomethingWithWhatYouCalculated).

  2. If, on the other hand, findStudentAndCalculate returns a promise, you can use it's then method to "do something with what you calculated". This will allow you to keep the code for calculating separate from the code that uses the calculation. EG:

    findStudentAndCalculate(name, calculateSomething).then(function (result) {
      // do something with result
    })
    

    That would be a very unusual way to structure a promise, though. Normally, the pattern would look more like this:

    findStudent(name).then(calculateSomething).then(function (result) {
      // do something with result
    })
    

Like talemyn said, though, we really need more information about findStudentAndCalculate before we can be more specific.

Community
  • 1
  • 1
Matt McMahon
  • 655
  • 5
  • 5