cowboydan cowboydan - 1 year ago 64
AngularJS Question

arrow functions and logging json response to console

I call my service and it hits an API and returns a json object. I log the object to console to view the structure of the json object and make sure it returns the data i want.

var promise = getJsonObjectService.getJsonObject();

promise.then(function (data) {
$ = data;

I recently learned of Arrow functions and replaced the previous code with this

var promise = getJsonObjectService.getJsonObject();

promise.then(data => $ = data);

The json object is returned and the page works the same as before, but I cannot figure out how to log the object to console like the previous method. Can you explain how i would log the json object returned in this arrow function? Can you explain maybe a little about what is happening differently under the hood as between these two code blocks?

Answer Source

This is the way Es6 array function used
promise.then((data) => { $ = data; console.log($; });

function(params ....){} is replaced by (params) => {} to provide outer function 'this' reference to inside function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download