Javascript: Promises: then

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

CustomPromise.js

The CustomPromise challenge series covers many topics, is closely related to the ECMA standard, and will boost even experienced developers. If the classes and late binding are still unfamiliar to you, then continue learning and return to these challenges later.

In this challenge, you will begin to implement a "lightweight" version of the Promise object with your bare hands. Subsequent challenges will develop the topic and augment it with new functionality.

You should only create a class in this challenge, without using asynchronicity, but with a chain of calls. To do this, implement a constructor in the CustomPromise class that takes the executor(resolve) callback and the then(callback) method;

Do not use the built-in Promise and the async keyword in your solution.

Usage examples

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

const promise = new CustomPromise((resolve) => resolve('Hello, world!'));
promise
  .then((value) => {
    console.log(value); // 'Hello, world!'
  });

const result = await promise
  .then((value) => value.replace('Hello', 'Goodbye'))
  .then((value) => value.toUpperCase());
console.log(result); // GOODBYE, WORLD!

Tips

  • You will need an auxiliary method to enclose the values
  • Study the use cases in the tests, they rely on the documented capabilities of promises.
  • The teacher's solution uses binding via bind(), but you can simplify the task by using arrow functions

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