QUESTION: Как создать Promise?
Creating a promise in JavaScript is done using the special new Promise() constructor. This constructor accepts as an argument an executor function, which developers often call the executor. This function runs automatically right after the promise is created and accepts two mandatory parameters that are themselves functions: resolve and reject.
The first function, resolve, is used for the successful completion of the operation. When the asynchronous task inside the executor body successfully completes, you call resolve(value), passing the obtained result to it. This transitions the promise from the pending state to the fulfilled state, making the value available to the then() method. The second function, reject, is called if an error occurred during the task execution or a condition was not met. You call reject(error), passing an error object, which transitions the promise to the rejected state and activates error handlers.
In addition to manually creating promises via the constructor using an executor, JavaScript provides useful static methods for quickly creating ready-made promises. For example, the Promise.resolve(value) method returns a promise that is already in the fulfilled state with the passed value. This is useful when you need to unify the interface of a function that in some cases might return a synchronous value, and in others an asynchronous promise.
The Promise.reject(error) method works similarly, immediately creating and returning a rejected promise with the specified error reason. Using these static methods significantly simplifies writing utility code and allows you to efficiently manage the flow of asynchronous data in modern applications without extra boilerplate.