Javascript: Promises: event loop

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

CustomPromise.js

This challenge continues the previous one by increasing the number of states and adding an asynchronous aspect.

Previously, you've implemented a reaction to a certain state, now it's time to add this state to the object and make the reaction asynchronous. Implement the then(onFulfill) method in the CustomPromise class and ensure asynchronous execution of the resolve(data) function.

Algorithm

Use the state names from the standard, we check them in our tests.

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 messages = [];

const resolvedPromise = new CustomPromise((resolve) => {
  resolve('Resolving first?');
});

const modifiedPromise = resolvedPromise
  .then(() => { messages.push('Change status first.'); });

await modifiedPromise
  .then(() => { messages.push('Then resolving.') });

console.log(messages.join(' ')); // Change status first, then resolve

Tips

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

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