Asynchronous javascript in Lightning Web Component

Being a scripting language, javascript can interpret only one statement at a time. With this, javscript also don't face concurrency issue like other language faces while executing threads.

Having said that, in javscript you can execute "Asynchronous Methods". These Asynchronous Methods are "Callbacks, Promises, Async/Await".

  1. Callbacks - a perfect example of callbacks in javascript is setTimeout function.

{	setTimeout(() => {
		alert('This is a callback Demo');
  }, 5000);

Javascript event also execute callback functions.

You can learn about event in lwc here.

2. Promises - These are used to perform asynchronous operation in javscript.

Description : A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

A promise can have three stages

- Pending - Default state, not fulfilled, not rejected.

- Fulfilled - Operation completed.