Why is catch function called twice on the same promise?

I’m trying to understand how promises work under the hood in JS and faced with this example:

const promise = Promise.reject();

promise
  .catch(() => console.log(1));
  
promise
  .catch(() => console.log(2));

// Output:
// 1
// 2
const promise = Promise.reject();

promise
  .catch(() => console.log(1))
  .catch(() => console.log(2));

// Output:
// 1

Why do we have different outputs in similar examples?
Why catch is called second time in the first example?
Because promise is already rejected after the first catch, so it should be logical that the second catch should not be called in both cases…
I think the main reason here is how microtasks queue works but I’m not sure.

New contributor

Pavel Orlov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

5

It boils down to this:

  • You can attach multiple callbacks to one promise. Yes, you can have several success and failure callbacks on the same promise, and they’ll all be called once the promises either resolves or rejects.

  • In a promise chain, a catch in the chain will catch the propagation of rejections and continue on the successful path afterwards.

Your first case is an example of a promise chain fork:

promise → catch → ...
      ↳ catch → ...

You attach two catches to the same promise. Both will be called if the promise rejects. If you attach more callbacks to one or both catches, they’ll be called depending on the result of your catch handler.

Your second case is a simple chain:

promise → catch → catch → ...

The first catch handler is called because the promise rejected. That handler doesn’t produce any new errors, so the chain continues to the next success handler. Of which there is none, so nothing else happens.

This behavior is due to the microtask queue and how promises chaining works in JavaScript.
in the first example
Promise.reject() creates a rejected promise. This immediately schedules the catch handlers attached to the promise in the microtask queue for execution after the current synchronous code has finished.

promise.catch(() => console.log(1));

This schedules a microtask for the first catch handler to handle the rejection and log 1.
promise.catch(() => console.log(2));

This schedules another microtask for the second catch handler to handle the same rejection and log 2

in the second example
Promise.reject() creates a rejected promise. The rejection is scheduled to be handled in the microtask queue by the first catch handler.

promise.catch(() => console.log(1))

The first catch is chained to the promise. When the rejection is handled here and logs 1, the promise returned by this catch becomes a resolved promise.
.catch(() => console.log(2))

This catch is attached to the promise returned by the previous catch. Since the previous catch has resolved the promise, this catch is not triggered.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Why is catch function called twice on the same promise?

I’m trying to understand how promises work under the hood in JS and faced with this example:

const promise = Promise.reject();

promise
  .catch(() => console.log(1));
  
promise
  .catch(() => console.log(2));

// Output:
// 1
// 2
const promise = Promise.reject();

promise
  .catch(() => console.log(1))
  .catch(() => console.log(2));

// Output:
// 1

Why do we have different outputs in similar examples?
Why catch is called second time in the first example?
Because promise is already rejected after the first catch, so it should be logical that the second catch should not be called in both cases…
I think the main reason here is how microtasks queue works but I’m not sure.

New contributor

Pavel Orlov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

5

It boils down to this:

  • You can attach multiple callbacks to one promise. Yes, you can have several success and failure callbacks on the same promise, and they’ll all be called once the promises either resolves or rejects.

  • In a promise chain, a catch in the chain will catch the propagation of rejections and continue on the successful path afterwards.

Your first case is an example of a promise chain fork:

promise → catch → ...
      ↳ catch → ...

You attach two catches to the same promise. Both will be called if the promise rejects. If you attach more callbacks to one or both catches, they’ll be called depending on the result of your catch handler.

Your second case is a simple chain:

promise → catch → catch → ...

The first catch handler is called because the promise rejected. That handler doesn’t produce any new errors, so the chain continues to the next success handler. Of which there is none, so nothing else happens.

This behavior is due to the microtask queue and how promises chaining works in JavaScript.
in the first example
Promise.reject() creates a rejected promise. This immediately schedules the catch handlers attached to the promise in the microtask queue for execution after the current synchronous code has finished.

promise.catch(() => console.log(1));

This schedules a microtask for the first catch handler to handle the rejection and log 1.
promise.catch(() => console.log(2));

This schedules another microtask for the second catch handler to handle the same rejection and log 2

in the second example
Promise.reject() creates a rejected promise. The rejection is scheduled to be handled in the microtask queue by the first catch handler.

promise.catch(() => console.log(1))

The first catch is chained to the promise. When the rejection is handled here and logs 1, the promise returned by this catch becomes a resolved promise.
.catch(() => console.log(2))

This catch is attached to the promise returned by the previous catch. Since the previous catch has resolved the promise, this catch is not triggered.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật