Ecma262: Does any JavaScript specification define the order of function execution where the arguments to the function are a nested array of function calls?

Created on 9 Jan 2019  路  2Comments  路  Source: tc39/ecma262

The title of the issue might not be technically correct. Am simply attempting to describe the result of code that encountered at Execute promise tree in order of declaration. If this is the incorrect place to ask, close the issue and kindly suggest where the question can be asked.

Consider

function promiseTree (name, children) {
  console.log(name, children, arguments[0], arguments); // arguments[0] is "great grandchild"
}

promiseTree('root', [
  promiseTree('child', [
    promiseTree('grandchild', [
      promiseTree('great grandchild')
    ])
  ])
])

Is the innermost call to the function, promiseTree('great grandchild'), specified to be executed first?

question

Most helpful comment

This seems answered - thanks, @getify!

All 2 comments

Is the innermost call to the function, promiseTree('great grandchild'), specified to be executed first?

Yes.

But it's simpler than the premise of your question, I think. Consider it this way... how would JS know what values it needs to pass in as arguments to the promiseTree('grandchild' ...) call, unless it had already finished the promiseTree('great grandchild') call? It has to execute that call, to get whatever value it returns (in this case, promiseTree(..) always returns undefined). That's not even really about the spec, it's just logical reasoning about what has to happen before something else.

Another way of visualizing this is looking at how that code is parsed into an AST (syntax tree):

screen shot 2019-01-08 at 9 52 43 pm

Evaluation of expressions (including function calls) has to happen following the direction of the arrow, because you can't know what one expression's node results in without knowing all the resolved values of its child expressions.

This seems answered - thanks, @getify!

Was this page helpful?
0 / 5 - 0 ratings