Javascript: Promises: reject

JS: Asynchronous programming
Last update: 24 Jan 23:20
0
Students

CustomPromise.js

This challenge continues the previous one, and extends the functionality of the executor function by adding the reject(data) callback.

Implement a constructor in the CustomPromise class that takes the executor(resolve, reject) callback and the then(onFulfill, onReject) method. The resolve(data) and reject(error) methods must be executed asynchronously.

Algorithm

You will need the last, third state to trigger the reactions accumulated by the corresponding promise state, the state object itself can now be implemented inside the constructor.

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.

Do not implement the catch() method, stick with then() here.

Usage examples

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

const promise = new CustomPromise((resolve, reject) => reject('Hello, world!'));
promise
  .then((value) => {
    console.log(value); // this handler will be omitted
  });

// pass the data from reject to resolve to make it easier
const result = await promise.then((x) => x.toUpperCase());
console.log(result); // 'HELLO, WORLD!'

Tips

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
130
courses
1000
exercises
2000+
hours of theory
3200
tests