0
> var score = 6;

> function test(callback) {
    var score0 = 16;
    console.log(score0);
    function callback() { return score0; }; 
    callback();
  }

> score = test()
    16

> score
    undefined

The appropriate value for score should be undefined. What should I be doing?

1
  • 1
    You're missing return statement in your test() function. return callback(); will do the job. Commented Oct 26, 2015 at 12:26

2 Answers 2

1

Change the last line of test() to:

return callback();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I got killed at a hackathon over this :)
0

Here is one way to have a callback return a value:

function tens(value, callback) {
   return callback(10*value)
}

var n = tens(100, function(result){ return result })

n
  1000

Here is another way:

function tens(value, callback) {
   return callback(10*value)
}

var n = tens(150,
  function(err, result) {
    if (err) throw err
    else return result
  })

n
 1500

The trouble with the way I posed the question is that

function test() {
   var score0 = 16;
   return score0;
}

would have worked just as well. And if this simplified syntax works just as well, then the function "callback" in my original question is not a callback function but a function that I called by coincidence "callback"

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.