1

I studying Promise in Javascript.

The two examples look similar, but they produce different results.

The difference is that each parameter of 'then' is used, but I don't know the exact cause.

code #1

const promise = new Promise((resolve, reject) => {
    reject("fail")
    resolve("success");
})
promise.then(
    onfulfilled => console.log(onfulfilled),
    onrejected => console.log(onrejected)
);

result : fail

code #2

const promise = new Promise((resolve, reject) => {
    reject("fail")
    resolve("success");
})
promise.then(
    (onfulfilled, onrejected) => console.log(onfulfilled, onrejected)
);

result : Uncaught (in promise) fail

Thank you.

MOGI
  • 11
  • 2

1 Answers1

2

It's how then works. the first parametere is a function with one argument and a second optional function to handle rejection.

in the first code you pass the 2 functions correctly un code 2 you are passing just one function with 2 parameters and so you are not handling the rejection

R4ncid
  • 6,944
  • 1
  • 4
  • 18
  • There was an error when I tried to deliver two because there was one parameter of callback function of 'onfulfilled'.Thank you I understand. – MOGI Apr 10 '22 at 10:55