I'm looking at how to use Puppeteer for testing and all the code examples I come across use the following syntax:
(async () => {
//function body
})();
Now my question is not just what this does, as I understand what an asynchronous function is. What I want to know is what does this syntax mean and how is it parsed? I'm new to Node.js and Puppeteer and I've never seen this before. None of the tutorials I've found explain what is going on here.
I found this which explains the purpose of the =>
operator. So it seems that async () =>
is like a shorthand for async function ()
? I'm still confused as to why the whole thing is surrounded by parentheses and then followed by another pair of parentheses.
MDN shows that you can declare an asynchronous function in javascript using async function fname() {...}
. This seems straightforward. Why then not use this syntax? What is the difference?