0

I have an exercise to make a promise which ends with failure which returns the string "Failure". I have to use then to manipulate the error.

The problem is that my output is just the reason of the reject and it doesn't even go to then and it doesn't console.log my string

My code:

const myPromise = new Promise((resolve, reject) => {
    reject("failure").then(console.log("ended with failure"))
})
console.log(myPromise)

What have I done wrong?

crazyfrog
  • 197
  • 1
  • 3
  • 11
  • 2
    rejections are handled by catch(), not then() – bel3atar May 11 '21 at 15:09
  • 3
    1. `.then(console.log("ended with failure"))` should be added to the promise, not to the `reject()` call 2. You should pass *a function* to `.then` - you're currently [just immediately logging something](https://stackoverflow.com/questions/50836242/how-does-thenconsole-log-and-then-console-log-in-a-promise-chain). 3. You still need `.catch()` – VLAZ May 11 '21 at 15:09
  • 1
    `myPromise.catch(console.log)` – Randy Casburn May 11 '21 at 15:10
  • 1
    so this is what I need to do? `const myPromise = new Promise((resolve, reject) => { reject("failure") }).then(() => console.log("ended with success")).catch(() => console.log("ENDED WITH FAILURE"))` – crazyfrog May 11 '21 at 15:15
  • the main problem, and something not one of the answers so far tells you, is that passed in `reject` callback in the Promise constructor doesn't return a promise - it returns `undefined` which is not a Promise – Jaromanda X May 11 '21 at 15:42

2 Answers2

2

The right way to do that will be.

const myPromise = new Promise((resolve, reject) => {
      reject("failure");
})
myPromise.catch(() => console.log("Caught an error!"));

resolve and reject are just functions that change the state of a promise, but not promises themself.

UPD: Please have look at this doc https://javascript.info/promise-basics it may help to learn promises.

Ayzrian
  • 2,279
  • 1
  • 7
  • 14
2

Promises are handled using .then() & .catch() method.

These methods are called exactly when the promise is either:

  • resolved,
  • rejected

Remember this:

  • Whenever a promise is resolved without any ERROR OR REJECTION, it will go in the .then method.
  • If there are error, exception or rejection, it will go in the .catch method.

lets look at an example of a promise being resolved successfully.

const myPromise = new Promise((resolve, reject) => {
  resolve("this will resolve this promise");
});

myPromise
  .then((elem) => console.log("Promise resolved came in .then block"))
  .catch((error) => console.log("promise with error came in .catch block ", error));

// prints 
// this will resolve this promise
// Promise resolved came in .then block

example # 2: Example of rejection:

// a rejected promise goes to .catch block
const myPromise = new Promise((resolve, reject) => {
  reject("this will reject this promise");
});

myPromise
  .then((elem) => console.log("Promise resolved came in .then block"))
  .catch((error) => console.log("promise with error came in .catch block ", error));

// prints 
// this will reject this promise
// promise with error came in .catch block 

example # 3: Example of Error:

// incase of error goes to .catch block
const myPromise = new Promise((resolve, reject) => {
  throw "sample exception";
});

myPromise
  .then((elem) => console.log("Promise resolved came in .then block"))
  .catch((error) => console.log("promise with error came in .catch block ", error));

// prints 
// promise with error came in .catch block 

Hopefully, by now you understand the purpose of these blocks now. If any questions please let me know.

Waleed Ahmad
  • 446
  • 3
  • 15