Skip to content

Commit

Permalink
Update docs & add js doc
Browse files Browse the repository at this point in the history
  • Loading branch information
doublelam committed Dec 19, 2024
1 parent a744b18 commit 8e7023c
Show file tree
Hide file tree
Showing 4 changed files with 1,812 additions and 2,555 deletions.
File renamed without changes.
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "promises-delivery",
"description": "This package is used to manage multiple promises by giving a key, you can call resolve or reject function out of the promise parameter callback and manage them by a key.",
"version": "1.0.0",
"version": "1.0.1",
"main": "./dist/index.js",
"type": "module",
"type": "module",
"repository": "https://github.com/doublelam/promises-delivery",
"types": "./types/index.d.ts",
"source": "./src/index.ts",
"packageManager": "yarn@4.2.2",
"packageManager": "yarn@1.22.22",
"devDependencies": {
"@eslint/compat": "1.1.1",
"@eslint/eslintrc": "3.2.0",
Expand All @@ -15,10 +16,17 @@
"eslint": "9.9.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsx-a11y": "6.9.0",
"eslint-plugin-prettier": "5.2.1",
"prettier": "3.3.3",
"typescript": "5.5.4"
},
"keywords": ["js", "promise", "deferred", "resolve", "reject"]
"license": "MIT",
"author": "Lam <[email protected]>",
"keywords": [
"js",
"promise",
"deferred",
"resolve",
"reject"
]
}
33 changes: 32 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ class Delivery<T> {
reject: (reason: string) => void;
};
} = {};
/**
* Registers a new promise with the given key.
*
* This method creates a new promise and stores it along with its resolve and reject functions.
* If a promise with the given key already exists, an error is thrown.
*
* @param key - The unique identifier for the promise to be registered.
* @throws Will throw an error if a promise with the given key is already registered.
* @returns A Promise<T> that can be used to handle the asynchronous operation.
*/
public register(key: string) {
if (this.promises[key]) {
throw new Error(`Promise with Key: ${key} is already registered`);
Expand Down Expand Up @@ -34,14 +44,35 @@ class Delivery<T> {
return this.promises[key].promise;
}

/**
* Resolves the promise associated with the given key with the provided value.
*
* @param key - The unique identifier for the promise to be resolved.
* @param value - The value to fulfill the promise with.
*
* @throws Will throw an error if a promise with the given key is not found.
*
* @returns {void}
*/
public resolve(key: string, value: T) {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
throw new Error(`Promise with Key: ${key} is not found`);
}
this.promises[key].resolve(value);
delete this.promises[key];
}

/**
* Rejects the promise associated with the given key with the provided reason.
*
* This method finds the promise with the specified key, rejects it with the given reason,
* and then removes it from the internal promises storage.
*
* @param key - The unique identifier for the promise to be rejected.
* @param reason - The reason for rejecting the promise.
* @throws Will throw an error if a promise with the given key is not found.
* @returns {void}
*/
public reject(key: string, reason: string) {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
Expand Down
Loading

0 comments on commit 8e7023c

Please sign in to comment.