To prevent promises from swallowing errors, ensure that you handle rejections using .catch()
or use try-catch
with async-await
syntax. Additionally, always return or throw errors explicitly.
Example:
Promise.resolve()
.then(() => { throw new Error('Error occurred'); })
.catch(error => console.error('Caught error:', error));
async function asyncFunction() {
try {
await Promise.reject('Error occurred');
} catch (error) {
console.error('Caught error:', error);
}
}
asyncFunction();
Tags: advanced, JavaScript, Promises