Thuta Learning
AdvancedProgrammingbeginner

JS Promises

Relax. We'll talk through this in plain words — no textbook voice.

Promises are objects representing the eventual result of an asynchronous operation. Once the operation finishes (succeed or fail), you can handle that result with the .then() (on success) and .catch() (on failure) methods.

javascript
const myPromise = new Promise((resolve, reject) => {
  let success = true;
  if (success) {
    resolve("The operation was successful!");
  } else {
    reject("The operation failed.");
  }
});

myPromise
  .then((message) => {
    console.log(message);
  })
  .catch((error) => {
    console.error(error);
  });
You should see
The operation was successful!
JS Promises | Thuta Learning