Javascript: Promises: catch

JS: Asynchronous programming
Last update: 25 Jan 23:20


This challenge continues the previous one and extends the promise object by adding the catch(onReject) method. You will learn how to catch and handle errors.

Implement the constructor of a CustomPromise class that takes the executor(resolve, reject) callback, the then(onFulfill, onReject) and catch(onReject) methods, and provide proper error handling navigating it to the relevant handlers.


The executor's reject(data) handler is called only when an error is thrown. The solution, in general, is similar to everything that has already been discussed earlier, but the implementation of the catch(onReject) method differs from then(onFulfill).

You can extend the teacher's solution from the previous exercise. Do not use the built-in Promise and the async keyword in your solution, use only timers and function calls within functions.

Usage examples

import CustomPromise from '../CustomPromise.js';

const resolvedPromise = new CustomPromise((resolve) => resolve('Hello, world!'));
  .then((value) => {
    console.log(value); // 'Hello, world!'
    throw new Error('Goodbye, world!');
  .catch((err) => console.error(err));  // 'Error: Goodbye, world!'

const rejectedPromise = new CustomPromise((_, reject) => reject('Hello, world!'));
const result = await rejectedPromise
  .catch((rejectMessage) => rejectMessage.split(' '))
  .then(([firstWord]) => firstWord + ' Pepe!');
console.log(result); // Hello, Pepe!


Study the use cases in the tests, they rely on the documented capabilities of promises.

You can dig deeper and learn some additional materials:

  • The standard we implement in this task

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
hours of theory