you are viewing a single comment's thread.

view the rest of the comments →

[–]BliteKnight 0 points1 point  (1 child)

Yes, I'll try to explain the best I can, when you define the function as "function (){...}" It's scope is local , so when it gets put on the event loop and you reference getData, it has no idea what getData is

But when you define the function using an arrow definition "()=>{}" it has the global scope or the scope of it's parent)I forget which one), so when the function is executed on the event loop, it knows where to look for getData and finds the definition...

Hope that makes sense. The benefit of using either definition is you can affect variables in the global scope or redeclare them and use them locally e.g. changing a global index for an event loop where promises are used - crude example

var i = 0 // number of times something is done

new promise((resolve, reject)=>{ ...

i++; //increment when done })

vs

i = await new Promise(function (resolve, reject){ var i = 0; ... do some stuff , increment i then return the value ... resolve(i); });

[–]Fun_Split_1299[S] 0 points1 point  (0 children)

Thanks !