Even thought I have returned an error why then block is executed ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Even thought I have returned an error why then block is executed ?

function func(state) { return new Promise((resolve, reject) => { if (state) { resolve("promise is resolved"); } else { reject("oops ! promise is rejected"); } }); } let promise = func(true); promise.then((data) => { console.log(data); return new Error('test') }).then((data) => { console.log(`success ${data}`); }).catch((err) => { console.log(err); })

29th Jan 2024, 11:36 AM
susi
susi - avatar
1 Answer
0
Instead of returning an error, you should throw an error of course by using the throw keyword.
30th Jan 2024, 12:57 PM
ODLNT
ODLNT - avatar