Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async IIFE vs. Thenable

Tags:

javascript

Immediately-Invoked Function Expression:

(async (myVal) => {
  try {
    // stuff
  } catch (err) {
    console.log(err);
  }
})(myVal)

Thenable:

Promise.resolve()
  .then(async (myVal) => {
    // stuff
  })
  .catch((err) => {
    console.log(err);
  });

The self-invoked runs quicker. Other than that, any specific advantage to utilize one over the other and in which situations?

like image 749
basickarl Avatar asked Sep 08 '26 05:09

basickarl


1 Answers

Some other differences:

  • It looks nicer
  • It uses myVal instead of undefined for the local myVal variable
  • It's syntax and doesn't rely on the global value of Promise or its methods
like image 189
Bergi Avatar answered Sep 10 '26 17:09

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!