1

I just spend long time digging through callbacks in promises wondering why some callbacks are not called. In the end problem was with incorrect declaration, instead of

       Promise.when(null).then(function () {
            var p = new Promise();
            p.fail(new Error("some message"));
            return p;
        }).then(function () {
            console.log("success");
        }, function (err) {
            console.log("failure");
        });

I did

       Promise.when(null).then(function () {
            var p = new Promise();
            p.fail(new Error("some message"));
            return p;
        }).then(function () {
            console.log("success");
        }), function (err) {
            console.log("failure");
        };

Regardless of Promise implementation details it boils down to one thing:

function(){};//throws SyntaxError
"something valid, or function call", function(){};//no error

I would like someone to explain this to me. Why first is throwing SyntaxError while latter is valid (at least in browser console)? It seems to declare anonymous function. Same thing happens when I try

eval("function(){};//throws SyntaxError")
eval("'lala',function(){};//no error")

So can someone explain me why first is not valid while latter is ?

yoosiba
  • 2,196
  • 1
  • 18
  • 27

2 Answers2

1

A statement that begins with the keyword "function" must be a valid function declaration statement. That requires a name for the function.

In an expression (or expression statement), that rule is different; no name is necessary because the function acts as a value in that context. No name is required then.

Thus this:

function myFunction() {
  // ...
}

is a function declaration statement. This:

5 + function() {};

is an expression statement (a silly one, but JavaScript is ok with that). Similarly, your example that results in no error is an expression with the comma operator:

"lala", function() {};

The keyword "function" does not appear at the beginning of either of those two expressions, so the parser does not insist on a name for the function.

yoosiba
  • 2,196
  • 1
  • 18
  • 27
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • continuation.(); was leftover after removing parts of original code. I removed it from code snippets. But thx, for the original answer. – yoosiba Jan 30 '13 at 19:35
0

Your original code was not working because, as you figured out, the second function was not passed as a parameter - you put the closing paren before the function. It was causing no error because JavaScript recognized it as a function expression. Your use of the comma operator told JavaScript to expect the next statement to be an expression.

A function declaration cannot be anonymous, but a function expression can. A stand alone anonymous function looks like a function declaration that is missing an identifier to JavaScript. But, combined with an operator, JavaScript treats an anonymous function as the operator's operand expression.

The statement 'lala',function(){} consists of an expression ('lala') followed by an operator (,) followed by a function expression. This statement is similar: 0,function(){}. Promise.when(...).then(...) is also a valid expression, so Promise.when(...).then(...),function(){} works just the same.

gilly3
  • 87,962
  • 25
  • 144
  • 176
  • continuation.(); was leftover after removing parts of original code. I removed it from code snippets. Actual question was about second part, so thx, for function declaration/expression clarification. – yoosiba Jan 30 '13 at 19:37
  • Neither of your corrected code snippets produce a syntax error. – gilly3 Jan 30 '13 at 19:49
  • That is the point. Latter Promise example is valid, but error handler is not actually passed to the Promise.then() and is never called. I was suprised why there was no error on function that follows last then() – yoosiba Jan 30 '13 at 19:52
  • OH! Your question is why your second `Promise` code *doesn't* cause a syntax error? – gilly3 Jan 30 '13 at 19:55
  • yep... maybe expressed in indirect way... but that was the question – yoosiba Jan 30 '13 at 20:00
  • Ok, I changed my answer to better address your question. – gilly3 Jan 30 '13 at 20:25