0

() => {} (); I don't know what that is.

(async () => {
   try {
      //await authentication
   } catch (error) {
      //catch errors
   }
})();

Can you do that? How does that even work? So this is a node.js piece of code where I'm learning how to attach a database to a node application and we're using sqlite along with async/await. To connect to the database, we're preparing to use the function above but I don't understand how you can even do it in javascript?

jfriend00
  • 683,504
  • 96
  • 985
  • 979
Voldy21
  • 39
  • 5
  • https://developer.mozilla.org/en-US/docs/Glossary/IIFE It's JS function that runs as soon as it is defined. – Colin Apr 27 '20 at 03:23
  • It’s an [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) (immediately invoked function expression). It defines a function and invokes it. – ray Apr 27 '20 at 03:25
  • `(() => { code here })` defines an anonymous arrow function. Adding the `()` on the end, calls it immediately without having to reference a name. – jfriend00 Apr 27 '20 at 04:46

1 Answers1

1
function abc = () => {

}

is a normal function in javascript, and to execute we need to add () before the function name similar to abc();

Now to make anonymous function we need to do function () => {} and to execute it we need to add (). So, execute it we wrap it inside () to make a block and add () at last to execute.

This will make syntax look lile :-

( () => {

})();

this is what we call an IIFE in javascript.

Now async await is another concept in js , where we make a function async and use await to make a function wait to resolve before continuing execution.

So, now our code will look like :-

(async() => {
   try{
     await authenticate();
   }catch(){

   }
})(); 
jfriend00
  • 683,504
  • 96
  • 985
  • 979
Atul Sharma
  • 9,397
  • 10
  • 38
  • 65