Promises are a way to handle asynchronous operations in JS. To truly understand how they work, I decided to implement my own version of promises.
class MyPromise {
// Constructor and private fields
#state; // Current state of the promise (pending, fulfilled, rejected)
#value; // Resolved value of the promise
#reason; // Reason for rejection
#fulfillReactions; // List of fulfill callbacks
#rejectReactions; // List of reject callbacks
constructor(executor) // Accepts a function to handle resolve and reject logic
// Private methods
get #isSettled(); // Checks if the promise is settled
#fireReactions(); // Executes the appropriate callbacks based on state
#onResolve(value); // Handles resolve logic
#onReject(reason); // Handles reject logic
// Public instance methods
then(onFulfilled, onRejected); // Registers fulfillment and rejection handlers
catch(onRejected); // Shortcut for handling rejection
finally(onFinally); // Executes logic regardless of fulfillment/rejection
// Public static methods
static resolve(value); // Creates a fulfilled promise with a given value
static reject(reason); // Creates a rejected promise with a given reason
static all(promises); // Resolves when all promises are fulfilled, or rejects on the first rejection
static any(promises); // Resolves with the first fulfilled promise or rejects when all are rejected
static race(promises); // Resolves or rejects with the first settled promise
static allSettled(promises); // Resolves when all promises settle (fulfilled or rejected)
}
The project is organized for simplicity:
src
contains the source code.src/index.js
holds the MyPromise class.test
contains the test cases..prettierrc
configures Prettier.package.json
manages project metadata.
To install the project, you can follow these steps:
-
Clone the repository using the following command:
git clone <repo_url>
-
Navigate to the project directory:
cd <project_name>
-
Install the dependencies:
npm install
-
Run the tests:
npm test
To use the MyPromise
class, you can follow these steps:
-
Import the
MyPromise
class:const MyPromise = require('./src/index');
-
Create a new instance of the
MyPromise
class:const promise = new MyPromise((resolve, reject) => { setTimeout(() => { resolve('Hello, World!'); }, 1000); });
-
Use the
then
method to handle the resolved value:promise.then((value) => { console.log(value); });
-
Use the
catch
method to handle the rejected value:promise.catch((error) => { console.error(error); });
-
Use the
finally
method to handle the resolved or rejected value:promise.finally(() => { console.log('Promise is settled'); });
Enjoy using the other methods of the MyPromise
class!
Contributions are welcome! You can:
- Add features
- Fix bugs
- Improve documentation
- Suggest ideas
This project is licensed under the MIT License - see the LICENSE file for details.