Home
Dmitri Pavlutin
I help developers understand Frontend technologies
Post cover

JavaScript Promises: then(f,f) vs then(f).catch(f)

Posted July 21, 2021

In JavaScript, you can access the fullfillment value or the rejection reason of a promise in 2 ways.

A) Use 2 callbacks on promise.then(fn, fn):


promise
.then(success, error);

B) Or use a chain of promise.then(fn).catch(fn):


promise
.then(success)
.catch(error);

Is there any difference between the 2 approaches? Let's find out!

1. What's the same

Let's consider the callbacks we're going to use:


function success(value) {
console.log('Resolved: ', value);
}
function error(err) {
console.log('Error: ', err);
}

success() function would be called on fulfillments, while error() on rejections.

In most cases both approaches work the same way: if promise resolves successfully, then success is called in both approaches:


Promise.resolve('Hi!')
.then(success, error);
// Logs 'Resolved: Hi!'
Promise.resolve('Hi!')
.then(success)
.catch(error);
// Logs 'Resolved: Hi!'

Open the demo.

Otherwise, in case of rejection, error callback is called:


Promise.reject('Oops!')
.then(success, error);
// Logs 'Error: Oops!'
Promise.reject('Oops!')
.then(success)
.catch(error);
// Logs 'Error: Oops!'

Open the demo.

In the above examples, the behavior of both approaches is the same.

2. What's the difference

The difference is seen when the success() callback of the resolved promise returns a rejected promise. That might happen when the resolved value is invalid.

Let's modify the success callback to return a rejected promise:


function rejectSuccess(invalidValue) {
console.log('Invalid success: ', invalidValue);
return Promise.reject('Invalid!');
}

Now let's use rejectSuccess in both approaches:


Promise.resolve('Zzz!')
.then(rejectSuccess, error);
// Logs 'Invalid success: Zzzzz!'
Promise.resolve('Zzz!')
.then(rejectSuccess)
.catch(error);
// Logs 'Invalid success: Zzzzz!'
// Logs 'Error: Invalid!'

Open the demo.

Promise.resolve('Zzz!').then(rejectSuccess, error) only calls rejectSuccess, even if rejectSuccess returns a rejected promise. error callback is not invoked.

Promise.resolve('Zzz!').then(rejectSuccess).catch(error) calls rejectSuccess because the promise is resolved. But rejectSuccess returns a rejected promise, — it is caugth by .catch(error) and the error callback is invoked. That's the difference.

3. When to use

That could be useful, for example, when you perform a fetch request to get a list of items, but the list must obligatory have at least one item.

So, in case if the list is empty, you could simply reject that list:


import axios from "axios";
axios("/list.json")
.then(response => {
const list = response.data;
if (list.length === 0) {
return Promise.reject(new Error("Empty list!"));
}
return list;
})
.catch((err) => {
console.log(err);
});

Open the demo.

In the above example .catch(error) would catch the request errors and the empty list error.

4. Conclusion

The main difference between the forms promise.then(success, error) and promise.then(success).catch(error) is that in case if success callback returns a rejected promise, then only the second form is going to catch that rejection.

两个鬼故事热带鱼种类带启的男孩起名义不容情结局哈利路亚是什么意思手机使用电脑网络汽车美容店起名同城无忧网卖农产品公司名字起名大全购买国风女孩起名字店怎样起名字好狗狗起名字招财的母狗嗤之以鼻的意思催弦拂柱与君饮929电影网名字名字起名子大全小宝宝还没出生起啥名字张筱雨大胆人40张工厂免费起名注册物业公司起个名字苹果7颜色特种军医男女孩起名大全诗经楚辞唐砖txt人民币外汇牌价医院财务制度6月27日是什么星座免费起名字大全女孩打分测男孩的英文名字起鲁迅代表作少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化