0

I have seen the following two codes on the internet and I can't really understand how they are being parsed and executed. BTW I am new to ES6.

return getUsername()
.then(function (username) {
    return getUser(username);
})
.then(function (user) {
});

Is it correct to interpret the above code as:

return getUsername().then(function(username){return getUser(username);}).then(function (user){});

Or Does it have some different meaning in the ES6?

Similarly in the following:

new Q(value)
.then(function(/*Success handler*/){}, function(/*Failure handler*/){})

Should it be interpreted as:

new Q(value).then(function(/*Success handler*/){},function(/*Failure handler*/){})
1
  • In general, white spaces have no meaning in JavaScript. So yes, they are the same. Commented Jan 4, 2017 at 17:50

2 Answers 2

2

Your 'interpretations' are simply the removal of line feeds and white spaces. The results are functionally identical to the more readable, multi-line versions.

Sign up to request clarification or add additional context in comments.

1 Comment

is my interpretation correct? because I don't really understand how multi-line code like those are parsed in JS. btw thanks for the reply :)
1

Your interpretation is correct but this coding style has nothing to do with ES6. It is simply part of the JS syntax.

Developers will often put chained methods onto separate lines to make what's happening to the target object clearer.

Read this article about method chaining and see the bottom of the article for an example of chained methods on different lines: https://schier.co/blog/2013/11/14/method-chaining-in-javascript.html

2 Comments

is it correct even for the second one (new Q(value)...)?
Yes, even for the second one.

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.