JavaScript: Promises: state

Last update: 19 Jan 23:20
0
Students

CustomPromise.js

This challenge continues the previous one, and now you will get familiar with automata-based programming.

Реализуете в классе CustomPromise метод then(callback) и вызов обработчиков на основе состояния объекта. Состояния запишите в объект STATES и используйте его вместо текстовых значений.

Algorithm

To solve this challenge, you will now need to walk through the standard to deal with naming and the general algorithm. It specifies the states to be transitioned between, the initial object state, and the "reactions" (functions) to the state change.

One state will be enough, to begin with, so implement its initialization and clearing. When an object's state changes, a reaction should follow, and if it is still in its initial state, reactions should accumulate.

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, and use only function calls within functions.

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

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