Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
dc53984a77 | |||
a4d6c38a4f | |||
af0f406746 | |||
73bf085376 | |||
70c5f15b4c | |||
ee9bbe021c |
27
changelog.md
27
changelog.md
@ -1,5 +1,32 @@
|
|||||||
# 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)
|
## 2025-01-19 - 4.2.0 - feat(cumulativedeferred)
|
||||||
Added subDefer method to CumulativeDeferred
|
Added subDefer method to CumulativeDeferred
|
||||||
|
|
||||||
|
17
package.json
17
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartpromise",
|
"name": "@push.rocks/smartpromise",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "4.2.0",
|
"version": "4.2.3",
|
||||||
"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",
|
||||||
@ -23,9 +23,9 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsbuild": "^2.2.0",
|
"@git.zone/tsbuild": "^2.2.0",
|
||||||
"@git.zone/tsrun": "^1.3.3",
|
"@git.zone/tsrun": "^1.3.3",
|
||||||
"@git.zone/tstest": "^1.0.77",
|
"@git.zone/tstest": "^1.0.92",
|
||||||
"@push.rocks/tapbundle": "^5.5.5",
|
"@push.rocks/tapbundle": "^5.5.6",
|
||||||
"@types/node": "^22.10.7"
|
"@types/node": "^22.10.10"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@ -53,5 +53,12 @@
|
|||||||
"typescript",
|
"typescript",
|
||||||
"asynchronous",
|
"asynchronous",
|
||||||
"utilities"
|
"utilities"
|
||||||
]
|
],
|
||||||
|
"pnpm": {
|
||||||
|
"onlyBuiltDependencies": [
|
||||||
|
"esbuild",
|
||||||
|
"mongodb-memory-server",
|
||||||
|
"puppeteer"
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
3535
pnpm-lock.yaml
generated
3535
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1,53 +1,57 @@
|
|||||||
import * as smartpromise from '../ts/index.js';
|
import * as smartpromise from '../ts/index.js';
|
||||||
|
|
||||||
// using Deferreds
|
|
||||||
// simple deferred;
|
|
||||||
const done = smartpromise.defer();
|
|
||||||
done.promise.then((stringArg) => {
|
|
||||||
console.log(stringArg);
|
|
||||||
});
|
|
||||||
done.resolve('hello'); // whenever you are ready
|
|
||||||
|
|
||||||
// using deferreds in async functions to cater callback style apis
|
|
||||||
const myAsyncFunction = async (): Promise<string> => {
|
|
||||||
const done = smartpromise.defer<string>(); // returns your typical Deferred object
|
|
||||||
setTimeout(() => {
|
|
||||||
done.resolve('hi'); // will throw type error for other types than string as argument ;)
|
|
||||||
}, 6000);
|
|
||||||
|
|
||||||
console.log(done.status); // logs "pending";
|
|
||||||
await done.promise;
|
|
||||||
console.log(done.status); // logs "fullfilled"
|
|
||||||
console.log(done.duration); // outputs the duration in millisenconds
|
|
||||||
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
let myAsyncFunction2 = async () => {
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
smartpromise
|
|
||||||
.rejectedPromise(`what a lovely error message`)
|
|
||||||
.then(
|
|
||||||
() => {
|
|
||||||
console.log('This never makes it to console');
|
|
||||||
} /*, alternatively put a reject function here */
|
|
||||||
)
|
|
||||||
.catch((err) => {
|
|
||||||
console.log(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
import { tap, expect } from '@push.rocks/tapbundle';
|
import { tap, expect } from '@push.rocks/tapbundle';
|
||||||
|
|
||||||
tap.test('runs through', async () => {});
|
tap.test('simple deferred should resolve with correct value', async () => {
|
||||||
|
const done = smartpromise.defer<string>();
|
||||||
|
let result: string | undefined;
|
||||||
|
|
||||||
|
done.promise.then((stringArg) => {
|
||||||
|
result = stringArg;
|
||||||
|
});
|
||||||
|
done.resolve('hello');
|
||||||
|
|
||||||
|
await done.promise;
|
||||||
|
expect(result).toEqual('hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('deferred should work with async functions and track status/duration', async () => {
|
||||||
|
const myAsyncFunction = async (): Promise<string> => {
|
||||||
|
const done = smartpromise.defer<string>();
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
done.resolve('hi');
|
||||||
|
}, 100); // reduced timeout for testing
|
||||||
|
|
||||||
|
expect(done.status).toEqual('pending');
|
||||||
|
await done.promise;
|
||||||
|
expect(done.status).toEqual('fulfilled');
|
||||||
|
expect(done.duration).toBeGreaterThan(0);
|
||||||
|
|
||||||
|
return done.promise;
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await myAsyncFunction();
|
||||||
|
expect(result).toEqual('hi');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('resolvedPromise should resolve immediately with correct value', async () => {
|
||||||
|
const message = `I'll get logged to console soon`;
|
||||||
|
const result = await smartpromise.resolvedPromise(message);
|
||||||
|
expect(result).toEqual(message);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('rejectedPromise should reject with correct error message', async () => {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
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.0',
|
version: '4.2.3',
|
||||||
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.'
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user