3

I started developing Typescript & C# not long ago, while formerly most of my development was with javascript+php. I am developing an angular 2 app, and created an HTTP service. The problem I am discussing here has more to do with Typescript than with angular 2.

in the following code:

 export class HttpRest{ constructor( private _http: Http, private userIdsx: UserIds, private _jsonFormater: JsonFormater ){} ... getGroupsList(){ return this._http.get('file.json') .map(res => res.json()); } ... } 

I am passing an anonymous lambda function with a single statement, and i want to add curly braces to create a function scope, but when I do that things get broken.

Im trying to refactor:

.map(res => res.json()); 

To:

.map((res) => { res.json() }); 

So I could do more operations, but this brakes the code and results in an undefined error on the JSON object that is defined by res.json().

The question is, why does adding curly braces brakes this simple function? Isn't lambda functions with scope curly braces a built in feature of the language?

1 Answer 1

4

Use the following. You need to return something.

.map((res) => { return res.json() }); 

With the concise body version (without curly brackets), it's implicitly done...

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

1 Comment

Thanks this solved it, I did not realize that return values could also be implicitly inferred, which sounds to me like a bad practice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.