Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 87b3356202 | |||
| d8b0da1826 | |||
| dc53984a77 | |||
| a4d6c38a4f | |||
| af0f406746 | |||
| 73bf085376 | |||
| 70c5f15b4c | |||
| ee9bbe021c |
@@ -0,0 +1,39 @@
|
|||||||
|
{
|
||||||
|
"@git.zone/cli": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "code.foss.global",
|
||||||
|
"gitscope": "push.rocks",
|
||||||
|
"gitrepo": "smartpromise",
|
||||||
|
"shortDescription": "promise and deferred utilities",
|
||||||
|
"description": "A TypeScript library for managing promises and Deferred constructs, simplifying asynchronous programming.",
|
||||||
|
"npmPackagename": "@push.rocks/smartpromise",
|
||||||
|
"license": "MIT",
|
||||||
|
"keywords": [
|
||||||
|
"promise",
|
||||||
|
"deferred",
|
||||||
|
"async",
|
||||||
|
"promisify",
|
||||||
|
"cumulative deferred",
|
||||||
|
"timeout",
|
||||||
|
"typescript",
|
||||||
|
"asynchronous",
|
||||||
|
"utilities"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"release": {
|
||||||
|
"registries": [
|
||||||
|
"https://verdaccio.lossless.digital",
|
||||||
|
"https://registry.npmjs.org"
|
||||||
|
],
|
||||||
|
"accessLevel": "public"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"@git.zone/tsdoc": {
|
||||||
|
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||||
|
},
|
||||||
|
"@ship.zone/szci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,40 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2026-05-01 - 4.2.4 - fix(typescript)
|
||||||
|
tighten TypeScript typings and modernize project tooling configuration
|
||||||
|
|
||||||
|
- replace custom async map implementation with Promise.all while preserving result order
|
||||||
|
- add explicit generic overloads and unknown-based typings across promise utilities and deferred classes
|
||||||
|
- enable noImplicitAny and update tests to use the current tstest tapbundle import
|
||||||
|
- refresh package and project config for newer git.zone tooling and npm release settings
|
||||||
|
|
||||||
|
## 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
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2026 Task Venture Capital GmbH <hello@task.vc>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
+14
-6
@@ -1,14 +1,11 @@
|
|||||||
{
|
{
|
||||||
"npmci": {
|
"@git.zone/cli": {
|
||||||
"npmGlobalTools": [],
|
|
||||||
"npmAccessLevel": "public"
|
|
||||||
},
|
|
||||||
"gitzone": {
|
|
||||||
"projectType": "npm",
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "code.foss.global",
|
"githost": "code.foss.global",
|
||||||
"gitscope": "push.rocks",
|
"gitscope": "push.rocks",
|
||||||
"gitrepo": "smartpromise",
|
"gitrepo": "smartpromise",
|
||||||
|
"shortDescription": "promise and deferred utilities",
|
||||||
"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.",
|
||||||
"npmPackagename": "@push.rocks/smartpromise",
|
"npmPackagename": "@push.rocks/smartpromise",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -23,9 +20,20 @@
|
|||||||
"asynchronous",
|
"asynchronous",
|
||||||
"utilities"
|
"utilities"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"release": {
|
||||||
|
"registries": [
|
||||||
|
"https://verdaccio.lossless.digital",
|
||||||
|
"https://registry.npmjs.org"
|
||||||
|
],
|
||||||
|
"accessLevel": "public"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"tsdoc": {
|
"@git.zone/tsdoc": {
|
||||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||||
|
},
|
||||||
|
"@ship.zone/szci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmRegistryUrl": "registry.npmjs.org"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+19
-10
@@ -1,31 +1,31 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartpromise",
|
"name": "@push.rocks/smartpromise",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "4.2.0",
|
"version": "4.2.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",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/ --web)",
|
"test": "tstest test/ --web",
|
||||||
"build": "(tsbuild --web --allowimplicitany)",
|
"format": "gitzone format",
|
||||||
|
"build": "tsbuild --web",
|
||||||
"buildDocs": "tsdoc"
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://code.foss.global/push.rocks/smartpromise.git"
|
"url": "https://code.foss.global/push.rocks/smartpromise.git"
|
||||||
},
|
},
|
||||||
"author": "Lossless GmbH",
|
"author": "Task Venture Capital GmbH <hello@task.vc>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
"url": "https://code.foss.global/push.rocks/smartpromise/issues"
|
||||||
},
|
},
|
||||||
"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": "^4.4.0",
|
||||||
"@git.zone/tsrun": "^1.3.3",
|
"@git.zone/tsrun": "^2.0.3",
|
||||||
"@git.zone/tstest": "^1.0.77",
|
"@git.zone/tstest": "^3.6.3",
|
||||||
"@push.rocks/tapbundle": "^5.5.5",
|
"@types/node": "^25.6.0"
|
||||||
"@types/node": "^22.10.7"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@@ -36,6 +36,8 @@
|
|||||||
"dist_ts_web/**/*",
|
"dist_ts_web/**/*",
|
||||||
"assets/**/*",
|
"assets/**/*",
|
||||||
"cli.js",
|
"cli.js",
|
||||||
|
".smartconfig.json",
|
||||||
|
"license",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
],
|
],
|
||||||
@@ -53,5 +55,12 @@
|
|||||||
"typescript",
|
"typescript",
|
||||||
"asynchronous",
|
"asynchronous",
|
||||||
"utilities"
|
"utilities"
|
||||||
|
],
|
||||||
|
"pnpm": {
|
||||||
|
"onlyBuiltDependencies": [
|
||||||
|
"esbuild",
|
||||||
|
"mongodb-memory-server",
|
||||||
|
"puppeteer"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Generated
+4198
-6518
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
import { expect, tap } from '@push.rocks/tapbundle';
|
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||||
import * as smartpromise from '../ts/index.js';
|
import * as smartpromise from '../ts/index.js';
|
||||||
|
|
||||||
tap.test('should return a Deferred for .defer()', async () => {
|
tap.test('should return a Deferred for .defer()', async () => {
|
||||||
@@ -17,7 +17,7 @@ tap.test('should let types flow through the Promise', async () => {
|
|||||||
|
|
||||||
tap.test('should map callbacks', async () => {
|
tap.test('should map callbacks', async () => {
|
||||||
const inputArray = ['hi', 'awesome'];
|
const inputArray = ['hi', 'awesome'];
|
||||||
const myPromisified = async (myInput) => {
|
const myPromisified = async (myInput: string) => {
|
||||||
return myInput;
|
return myInput;
|
||||||
};
|
};
|
||||||
const result = await smartpromise.map(inputArray, myPromisified);
|
const result = await smartpromise.map(inputArray, myPromisified);
|
||||||
|
|||||||
+41
-37
@@ -1,53 +1,57 @@
|
|||||||
import * as smartpromise from '../ts/index.js';
|
import * as smartpromise from '../ts/index.js';
|
||||||
|
import { tap, expect } from '@git.zone/tstest/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) => {
|
||||||
console.log(stringArg);
|
result = stringArg;
|
||||||
});
|
});
|
||||||
done.resolve('hello'); // whenever you are ready
|
done.resolve('hello');
|
||||||
|
|
||||||
// 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;
|
await done.promise;
|
||||||
console.log(done.status); // logs "fullfilled"
|
expect(result).toEqual('hello');
|
||||||
console.log(done.duration); // outputs the duration in millisenconds
|
});
|
||||||
|
|
||||||
|
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;
|
return done.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
let myAsyncFunction2 = async () => {
|
const result = await myAsyncFunction();
|
||||||
let aString = await myAsyncFunction();
|
expect(result).toEqual('hi');
|
||||||
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
|
tap.test('resolvedPromise should resolve immediately with correct value', async () => {
|
||||||
.rejectedPromise(`what a lovely error message`)
|
const message = `I'll get logged to console soon`;
|
||||||
.then(
|
const result = await smartpromise.resolvedPromise(message);
|
||||||
() => {
|
expect(result).toEqual(message);
|
||||||
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';
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
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.0',
|
version: '4.2.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.'
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-24
@@ -3,23 +3,24 @@ import { defer } from './smartpromise.classes.deferred.js';
|
|||||||
export * from './smartpromise.classes.cumulativedeferred.js';
|
export * from './smartpromise.classes.cumulativedeferred.js';
|
||||||
export * from './smartpromise.classes.deferred.js';
|
export * from './smartpromise.classes.deferred.js';
|
||||||
|
|
||||||
|
export function resolvedPromise(): Promise<void>;
|
||||||
|
export function resolvedPromise<T>(value: T): Promise<T>;
|
||||||
/**
|
/**
|
||||||
* Creates a new resolved promise for the provided value.
|
* Creates a new resolved promise for the provided value.
|
||||||
*/
|
*/
|
||||||
export const resolvedPromise = <T>(value?: T): Promise<T> => {
|
export function resolvedPromise<T>(value?: T): Promise<T | void> {
|
||||||
return Promise.resolve(value);
|
return Promise.resolve(value);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new rejected promise for the provided reason.
|
* Creates a new rejected promise for the provided reason.
|
||||||
*/
|
*/
|
||||||
export const rejectedPromise = (err) => {
|
export const rejectedPromise = (err: unknown): Promise<never> => {
|
||||||
return Promise.reject(err);
|
return Promise.reject(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
interface IAsyncFunction<T> {
|
interface IAsyncFunction<TInput, TOutput> {
|
||||||
(someArg: T): Promise<T>;
|
(someArg: TInput): Promise<TOutput>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,26 +29,19 @@ interface IAsyncFunction<T> {
|
|||||||
* @param inputArg
|
* @param inputArg
|
||||||
* @param functionArg
|
* @param functionArg
|
||||||
*/
|
*/
|
||||||
export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
export const map = async <TInput, TOutput = TInput>(
|
||||||
const promiseArray: Promise<any>[] = [];
|
inputArg: TInput[],
|
||||||
const resultArray = [];
|
functionArg: IAsyncFunction<TInput, TOutput>
|
||||||
for (const item of inputArg) {
|
): Promise<TOutput[]> => {
|
||||||
const promise: Promise<any> = functionArg(item);
|
return Promise.all(inputArg.map(functionArg));
|
||||||
promiseArray.push(promise);
|
|
||||||
promise.then((x) => {
|
|
||||||
resultArray.push(x);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await Promise.all(promiseArray);
|
|
||||||
return resultArray;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const timeoutWrap = async <T = any>(
|
export const timeoutWrap = async <T = unknown>(
|
||||||
promiseArg: Promise<T>,
|
promiseArg: Promise<T>,
|
||||||
timeoutInMsArg: number,
|
timeoutInMsArg: number,
|
||||||
rejectArg = true
|
rejectArg = true
|
||||||
) => {
|
): Promise<T | null> => {
|
||||||
return new Promise<T>((resolve, reject) => {
|
return new Promise<T | null>((resolve, reject) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (rejectArg) {
|
if (rejectArg) {
|
||||||
reject(new Error('timeout'));
|
reject(new Error('timeout'));
|
||||||
@@ -59,14 +53,14 @@ export const timeoutWrap = async <T = any>(
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const timeoutAndContinue = async <T = any>(
|
export const timeoutAndContinue = async <T = unknown>(
|
||||||
promiseArg: Promise<T>,
|
promiseArg: Promise<T>,
|
||||||
timeoutInMsArg = 60000
|
timeoutInMsArg = 60000
|
||||||
) => {
|
): Promise<T | null> => {
|
||||||
return timeoutWrap(promiseArg, timeoutInMsArg, false);
|
return timeoutWrap(promiseArg, timeoutInMsArg, false);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
|
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]): Promise<boolean> => {
|
||||||
const done = defer<boolean>();
|
const done = defer<boolean>();
|
||||||
for (const promiseArg of promisesArg) {
|
for (const promiseArg of promisesArg) {
|
||||||
promiseArg.then((resultArg) => {
|
promiseArg.then((resultArg) => {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { defer } from "./smartpromise.classes.deferred.js";
|
import { defer } from './smartpromise.classes.deferred.js';
|
||||||
|
|
||||||
export class CumulativeDeferred {
|
export class CumulativeDeferred {
|
||||||
private accumulatedPromises: Promise<any>[] = [];
|
private accumulatedPromises: Promise<unknown>[] = [];
|
||||||
private deferred = defer();
|
private deferred = defer<void>();
|
||||||
public promise = this.deferred.promise;
|
public promise = this.deferred.promise;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -16,17 +16,16 @@ export class CumulativeDeferred {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public subDefer() {
|
public subDefer() {
|
||||||
const done = defer();
|
const done = defer<void>();
|
||||||
this.addPromise(done.promise);
|
this.addPromise(done.promise);
|
||||||
return done;
|
return done;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addPromise(promiseArg: Promise<any>) {
|
public addPromise(promiseArg: Promise<unknown>): void {
|
||||||
this.accumulatedPromises.push(promiseArg);
|
this.accumulatedPromises.push(promiseArg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const cumulativeDefer = () => {
|
export const cumulativeDefer = () => {
|
||||||
return new CumulativeDeferred();
|
return new CumulativeDeferred();
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -3,16 +3,16 @@ export interface IResolve<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface IReject {
|
export interface IReject {
|
||||||
(reason?: any): void;
|
(reason?: unknown): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
|
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
|
||||||
|
|
||||||
export class Deferred<T> {
|
export class Deferred<T> {
|
||||||
public promise: Promise<T>;
|
public promise: Promise<T>;
|
||||||
public resolve: IResolve<T>;
|
public resolve!: IResolve<T>;
|
||||||
public reject: IReject;
|
public reject!: IReject;
|
||||||
public status: TDeferredStatus;
|
public status!: TDeferredStatus;
|
||||||
public claimed = false;
|
public claimed = false;
|
||||||
public claim() {
|
public claim() {
|
||||||
if (this.claimed) {
|
if (this.claimed) {
|
||||||
@@ -21,8 +21,8 @@ export class Deferred<T> {
|
|||||||
this.claimed = true;
|
this.claimed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public startedAt: number;
|
public startedAt!: number;
|
||||||
public stoppedAt: number;
|
public stoppedAt!: number;
|
||||||
public get duration(): number {
|
public get duration(): number {
|
||||||
if (this.stoppedAt) {
|
if (this.stoppedAt) {
|
||||||
return this.stoppedAt - this.startedAt;
|
return this.stoppedAt - this.startedAt;
|
||||||
@@ -33,12 +33,12 @@ export class Deferred<T> {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.promise = new Promise<T>((resolve, reject) => {
|
this.promise = new Promise<T>((resolve, reject) => {
|
||||||
this.resolve = (valueArg: T | PromiseLike<T>) => {
|
this.resolve = (valueArg?: T | PromiseLike<T>) => {
|
||||||
this.status = 'fulfilled';
|
this.status = 'fulfilled';
|
||||||
this.stoppedAt = Date.now();
|
this.stoppedAt = Date.now();
|
||||||
resolve(valueArg);
|
resolve(valueArg as T | PromiseLike<T>);
|
||||||
};
|
};
|
||||||
this.reject = (reason: any) => {
|
this.reject = (reason?: unknown) => {
|
||||||
this.status = 'rejected';
|
this.status = 'rejected';
|
||||||
this.stoppedAt = Date.now();
|
this.stoppedAt = Date.now();
|
||||||
reject(reason);
|
reject(reason);
|
||||||
@@ -49,6 +49,6 @@ export class Deferred<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const defer = <T>() => {
|
export const defer = <T = void>() => {
|
||||||
return new Deferred<T>();
|
return new Deferred<T>();
|
||||||
};
|
};
|
||||||
+4
-4
@@ -5,10 +5,10 @@
|
|||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"module": "NodeNext",
|
"module": "NodeNext",
|
||||||
"moduleResolution": "NodeNext",
|
"moduleResolution": "NodeNext",
|
||||||
|
"noImplicitAny": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"verbatimModuleSyntax": true
|
"verbatimModuleSyntax": true,
|
||||||
|
"types": ["node"]
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": ["dist_*/**/*.d.ts"]
|
||||||
"dist_*/**/*.d.ts"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user