Compare commits
No commits in common. "master" and "v4.0.4" have entirely different histories.
40
changelog.md
40
changelog.md
@ -1,42 +1,5 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## 2025-02-20 - 4.2.3 - fix(test)
|
|
||||||
Refactor test suite for improved readability and maintainability.
|
|
||||||
|
|
||||||
- Updated and cleaned up test cases for deferreds and promises.
|
|
||||||
- Reduced timeouts in tests for quicker execution.
|
|
||||||
- Added expectations for promise status and duration.
|
|
||||||
|
|
||||||
## 2025-02-20 - 4.2.3 - fix(test)
|
|
||||||
Refactor test suite for improved readability and maintainability.
|
|
||||||
|
|
||||||
- Updated and cleaned up test cases for deferreds and promises.
|
|
||||||
- Reduced timeouts in tests for quicker execution.
|
|
||||||
- Added expectations for promise status and duration.
|
|
||||||
|
|
||||||
## 2025-01-23 - 4.2.2 - fix(dependencies)
|
|
||||||
Update @git.zone/tstest and @types/node dependencies to latest versions
|
|
||||||
|
|
||||||
- Updated @git.zone/tstest from ^1.0.91 to ^1.0.92
|
|
||||||
- Updated @types/node from ^22.10.9 to ^22.10.10
|
|
||||||
|
|
||||||
## 2025-01-23 - 4.2.1 - fix(devDependencies)
|
|
||||||
Update development dependencies to latest versions
|
|
||||||
|
|
||||||
- Updated @git.zone/tstest to version ^1.0.91
|
|
||||||
- Updated @push.rocks/tapbundle to version ^5.5.6
|
|
||||||
- Updated @types/node to version ^22.10.9
|
|
||||||
|
|
||||||
## 2025-01-19 - 4.2.0 - feat(cumulativedeferred)
|
|
||||||
Added subDefer method to CumulativeDeferred
|
|
||||||
|
|
||||||
- Introduced `subDefer` method in CumulativeDeferred class to allow creating and adding new Deferreds easily.
|
|
||||||
|
|
||||||
## 2025-01-07 - 4.1.0 - feat(core)
|
|
||||||
Add fromCallback utility function for promisifying Node.js-style callback functions
|
|
||||||
|
|
||||||
- Added fromCallback function to convert Node.js-style callbacks into Promises.
|
|
||||||
|
|
||||||
## 2024-06-23 - 4.0.4 - fix(ci)
|
## 2024-06-23 - 4.0.4 - fix(ci)
|
||||||
Remove .gitlab-ci.yml and update dependencies and metadata
|
Remove .gitlab-ci.yml and update dependencies and metadata
|
||||||
|
|
||||||
@ -44,6 +7,8 @@ Remove .gitlab-ci.yml and update dependencies and metadata
|
|||||||
- Updated dependencies in package.json and npmextra.json
|
- Updated dependencies in package.json and npmextra.json
|
||||||
- Improved description and keywords for better package definition
|
- Improved description and keywords for better package definition
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 2024-05-29 - 4.0.3 - Maintenance
|
## 2024-05-29 - 4.0.3 - Maintenance
|
||||||
Update project configuration and descriptions.
|
Update project configuration and descriptions.
|
||||||
|
|
||||||
@ -120,3 +85,4 @@ Added polyfill and npmextra.json.
|
|||||||
Updated continuous integration configuration.
|
Updated continuous integration configuration.
|
||||||
|
|
||||||
- Update ci
|
- Update ci
|
||||||
|
```
|
||||||
|
19
package.json
19
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartpromise",
|
"name": "@push.rocks/smartpromise",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "4.2.3",
|
"version": "4.0.4",
|
||||||
"description": "A TypeScript library for managing promises and Deferred constructs, simplifying asynchronous programming.",
|
"description": "A TypeScript library for managing promises and Deferred constructs, simplifying asynchronous programming.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
@ -21,11 +21,11 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://code.foss.global/push.rocks/smartpromise",
|
"homepage": "https://code.foss.global/push.rocks/smartpromise",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsbuild": "^2.2.0",
|
"@git.zone/tsbuild": "^2.1.66",
|
||||||
"@git.zone/tsrun": "^1.3.3",
|
"@git.zone/tsrun": "^1.2.44",
|
||||||
"@git.zone/tstest": "^1.0.92",
|
"@git.zone/tstest": "^1.0.77",
|
||||||
"@push.rocks/tapbundle": "^5.5.6",
|
"@push.rocks/tapbundle": "^5.0.8",
|
||||||
"@types/node": "^22.10.10"
|
"@types/node": "^20.14.8"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@ -53,12 +53,5 @@
|
|||||||
"typescript",
|
"typescript",
|
||||||
"asynchronous",
|
"asynchronous",
|
||||||
"utilities"
|
"utilities"
|
||||||
],
|
|
||||||
"pnpm": {
|
|
||||||
"onlyBuiltDependencies": [
|
|
||||||
"esbuild",
|
|
||||||
"mongodb-memory-server",
|
|
||||||
"puppeteer"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
6009
pnpm-lock.yaml
generated
6009
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1,57 +1,53 @@
|
|||||||
import * as smartpromise from '../ts/index.js';
|
import * as smartpromise from '../ts/index.js';
|
||||||
import { tap, expect } from '@push.rocks/tapbundle';
|
|
||||||
|
|
||||||
tap.test('simple deferred should resolve with correct value', async () => {
|
|
||||||
const done = smartpromise.defer<string>();
|
|
||||||
let result: string | undefined;
|
|
||||||
|
|
||||||
|
// using Deferreds
|
||||||
|
// simple deferred;
|
||||||
|
const done = smartpromise.defer();
|
||||||
done.promise.then((stringArg) => {
|
done.promise.then((stringArg) => {
|
||||||
result = stringArg;
|
console.log(stringArg);
|
||||||
});
|
});
|
||||||
done.resolve('hello');
|
done.resolve('hello'); // whenever you are ready
|
||||||
|
|
||||||
await done.promise;
|
// using deferreds in async functions to cater callback style apis
|
||||||
expect(result).toEqual('hello');
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('deferred should work with async functions and track status/duration', async () => {
|
|
||||||
const myAsyncFunction = async (): Promise<string> => {
|
const myAsyncFunction = async (): Promise<string> => {
|
||||||
const done = smartpromise.defer<string>();
|
const done = smartpromise.defer<string>(); // returns your typical Deferred object
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
done.resolve('hi');
|
done.resolve('hi'); // will throw type error for other types than string as argument ;)
|
||||||
}, 100); // reduced timeout for testing
|
}, 6000);
|
||||||
|
|
||||||
expect(done.status).toEqual('pending');
|
console.log(done.status); // logs "pending";
|
||||||
await done.promise;
|
await done.promise;
|
||||||
expect(done.status).toEqual('fulfilled');
|
console.log(done.status); // logs "fullfilled"
|
||||||
expect(done.duration).toBeGreaterThan(0);
|
console.log(done.duration); // outputs the duration in millisenconds
|
||||||
|
|
||||||
return done.promise;
|
return done.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = await myAsyncFunction();
|
let myAsyncFunction2 = async () => {
|
||||||
expect(result).toEqual('hi');
|
let aString = await myAsyncFunction();
|
||||||
|
console.log(aString); // will log 'hi' to console
|
||||||
|
};
|
||||||
|
|
||||||
|
myAsyncFunction2();
|
||||||
|
|
||||||
|
// Resolved and Rejected promises
|
||||||
|
// ------------------------------------------------
|
||||||
|
smartpromise.resolvedPromise(`I'll get logged to console soon`).then((x) => {
|
||||||
|
console.log(x);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('resolvedPromise should resolve immediately with correct value', async () => {
|
smartpromise
|
||||||
const message = `I'll get logged to console soon`;
|
.rejectedPromise(`what a lovely error message`)
|
||||||
const result = await smartpromise.resolvedPromise(message);
|
.then(
|
||||||
expect(result).toEqual(message);
|
() => {
|
||||||
|
console.log('This never makes it to console');
|
||||||
|
} /*, alternatively put a reject function here */
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('rejectedPromise should reject with correct error message', async () => {
|
import { tap, expect } from '@push.rocks/tapbundle';
|
||||||
const errorMessage = 'what a lovely error message';
|
|
||||||
let caught = false;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await smartpromise.rejectedPromise(errorMessage);
|
|
||||||
} catch (err) {
|
|
||||||
caught = true;
|
|
||||||
expect(err).toEqual(errorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(caught).toEqual(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
tap.test('runs through', async () => {});
|
||||||
export default tap.start();
|
export default tap.start();
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartpromise',
|
name: '@push.rocks/smartpromise',
|
||||||
version: '4.2.3',
|
version: '4.0.4',
|
||||||
description: 'A TypeScript library for managing promises and Deferred constructs, simplifying asynchronous programming.'
|
description: 'A TypeScript library for managing promises and Deferred constructs, simplifying asynchronous programming.'
|
||||||
}
|
}
|
||||||
|
19
ts/index.ts
19
ts/index.ts
@ -80,22 +80,3 @@ export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
|
|||||||
});
|
});
|
||||||
return done.promise;
|
return done.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts a Node.js-style callback-based function into a Promise.
|
|
||||||
* @param fn The function that expects a callback.
|
|
||||||
* @returns A Promise that resolves with the result of the function or rejects with an error.
|
|
||||||
*/
|
|
||||||
export const fromCallback = <T>(
|
|
||||||
fn: (callback: (err: NodeJS.ErrnoException | null, result?: T) => void) => void
|
|
||||||
): Promise<T> => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
fn((err, result) => {
|
|
||||||
if (err) {
|
|
||||||
reject(err); // Reject the promise with the error
|
|
||||||
} else {
|
|
||||||
resolve(result as T); // Resolve the promise with the result
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
@ -15,12 +15,6 @@ export class CumulativeDeferred {
|
|||||||
}, 0);
|
}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public subDefer() {
|
|
||||||
const done = defer();
|
|
||||||
this.addPromise(done.promise);
|
|
||||||
return done;
|
|
||||||
}
|
|
||||||
|
|
||||||
public addPromise(promiseArg: Promise<any>) {
|
public addPromise(promiseArg: Promise<any>) {
|
||||||
this.accumulatedPromises.push(promiseArg);
|
this.accumulatedPromises.push(promiseArg);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user