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.
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