0

The jsfiddle.net code below produces Uncaught SyntaxError: Unexpected token ( and the text "Here", but does not produce the result I wish to study, which will help me understand callbacks in functions and how their return value, true/false in my case, can be used to determine which of two things are to be computed.

I would like help removing the syntax error so I can see the result.

function checkPos(input, callback) {
    if (input > 0)
    function () {
        callback(true);
    } else
    function () {
        callback(false);
    }
}

function dosomething(number) {
    var diff = 10 - number;
    checkPos(diff, function (e) {
        return e;
    });
}

var count = 1;
var multiplier = 6;
var result;
if (dosomething(11)) result = 5 + count
else result = 5 - count

document.getElementById("demo").innerHTML = String(result*multiplier);

The html code is simply as follows.

<body>Here
    <p id="demo"></p>
</body>
zerowords
  • 2,915
  • 4
  • 29
  • 44
  • What are these `function` keywords doing there in `checkPos`? I mean, what are they *supposed* to do there? – Bergi Jul 20 '14 at 20:03
  • The `return` value of a callback function is returned to where it was *called*, not where the function was defined. Your `doSomething` won't work. – Bergi Jul 20 '14 at 20:04
  • Yes, as you can see, I don't know how to fix that. Do you know how? – zerowords Jul 20 '14 at 21:31
  • Give `doSomething` a callback parameter as well, which you call from the other callback. See also http://stackoverflow.com/q/14220321/1048572 and possibly http://stackoverflow.com/q/23667086/1048572. If `checkPos` is not asynchronous, remove the callback style altogether and just `return`. – Bergi Jul 20 '14 at 21:49
  • You are right in suggesting to remove the callback-style at all. But you are wrong in assuming, some method has to be asynchronous to use callbacks; although it makes more sense, if it were. – Thomas Junk Jul 20 '14 at 22:29
  • My purpose in asking this question was to learn to use an asynchronous callback utility that I thought I would need to use to learn when a styled map marker system is actually dropped because it is now "deprecated" by google. However, upon further research, I was able to get myself on a relevant google forum mailing list with automatic mail updates. So I think I can omit the synch utility which would have automatically checked for system failure and wait instead for an email warning. – zerowords Jul 21 '14 at 15:51

3 Answers3

3

What you wrote is

function checkPos(input, callback) {
    if (input > 0)
    function () {
        callback(true);
    } else
    function () {
        callback(false);
    }
}    

That is a Syntax Error. You could not define a function in the midst of an if-statement

But you can call your callback:

function checkPos(input, callback) {
    if (input > 0)
        callback(true);
    else
        callback(false);
}

Here is the updated Fiddle

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
  • Something is apparently wrong. I always get the result 24, and never 36, which would be the result if `10-number` were positive, for example, if number were -3. What's wrong? – zerowords Jul 20 '14 at 20:42
  • 1
    Your function doSomething is wrong. Especially this line: checkPos(diff, function (e) { return e; }); – Thomas Junk Jul 20 '14 at 20:46
  • Right. So how do I get the true/false result back where I need it? – zerowords Jul 20 '14 at 21:01
  • Maybe try returning something ... return checkPos(diff, function (e) { return e; }); – Thomas Junk Jul 20 '14 at 21:05
  • @ThomasJunk: That's what he does, and which does obviously not work. – Bergi Jul 20 '14 at 21:47
  • @Bergi Where and what does the `doSomething` function return? I only see calling a function with an anonymous callback function, which returns to the scope to 'doSomething` and nothing is done. – Thomas Junk Jul 20 '14 at 22:25
  • Yes, that's the problem, so `return e` is not useful. And `checkPos()` doesn't return anything either. – Bergi Jul 20 '14 at 22:40
  • 1
    Oh, you pointed to the right point:return checkPos(diff, function (e) { return e; }); is one half and `checkPos` should return the result of the callback. That should do the trick. http://jsfiddle.net/LgcaX/4/ – Thomas Junk Jul 20 '14 at 22:45
  • I just saw this successful implementation. I could not get it before from your comment, but maybe I did it wrong. – zerowords Jul 21 '14 at 17:08
0

You need to name your functions if they are not attached to a variable ex.

function()
//not okay
var abc=function()
//acceptable
B_Troutman
  • 273
  • 1
  • 3
  • 4
0

if/if-else statement in javascript/ecmascript has the form:

if ( Expression ) Statement else Statement

if ( Expression ) Statement

You used function expression in statement's location, which according to the grammar isn't correct and hence the error.

Community
  • 1
  • 1
soulcheck
  • 36,297
  • 6
  • 91
  • 90