promises

Promises in JavaScript are a way to handle asynchronous operations. They represent a future value or error that will be available at some point in time. Promises have three states:

1. Pending: Initial state, neither fulfilled nor rejected.

2. Fulfilled: Meaning that the operation completed successfully, and the promise now has a resulting value.

3. Rejected: Meaning that the operation failed, and the promise has an associated error.

I promise to return something in the future


const promise = new Promise((resolve, reject) => {
  let fileLoaded = false;
  if (fileLoaded) {
    resolve("File loaded");
  } else {
    reject("File NOT loaded");
  }
}); // if this asynchronous process is successful invoke resolve, if not reject

promise
  .then((value) => console.log(value)) //if promise is resolved then
  .catch((error) => console.log(error)); // if promise is rejected then
      

const promise2 = new Promise((resolve) => {
  let x = false;
  if (x) {
    resolve("TRUE");
  }
});

promise2.then((value) => console.log(value));
      

const wait = (time) =>
  new Promise((resolve) => {
    setTimeout(resolve, time);
  });

wait(3000).then(() => console.log("Thanks for waiting"));