ok i'm asking this again, why the catch in the following code is not working as expected ? is this a design flaw of js ?
const getUserById = (id, authorized) => {
return new Promise((resolve, reject) => {
if (!authorized) {
reject('Unauthorized access to the user data');
}
resolve({
id: id,
username: 'admin'
});
});
}
try {
getUserById(10, false)
.then(user => console.log(user.username));
// the following code still executes ?
console.log('next');
} catch (error) {
console.log(`Caught by try/catch ${error}`);
}
Surprisingly, the catch didn't work.
The theory of try/catch block is catch should be able to caught all issues from try block. If we redesign JavaScript, can we make the catch in above code to work ?