Compare commits
32 Commits
Author | SHA1 | Date | |
---|---|---|---|
dc53984a77 | |||
a4d6c38a4f | |||
af0f406746 | |||
73bf085376 | |||
70c5f15b4c | |||
ee9bbe021c | |||
45d53eea31 | |||
d54b5790a3 | |||
a941ca69b3 | |||
1afbfd8895 | |||
3802c03838 | |||
8c18dcd0e6 | |||
b8e7063bab | |||
85706e8a4e | |||
3a5ea9aa13 | |||
4d762353dd | |||
f176e991a8 | |||
1f1e0e4ee1 | |||
139d4cb56d | |||
8038e54f53 | |||
37284da2cc | |||
b0772e2372 | |||
f3eac4a5f7 | |||
fde31ec25d | |||
810b299caa | |||
c71ac16090 | |||
91b52982c0 | |||
97f8327fe9 | |||
42f40449ad | |||
960c23fecd | |||
40642fd6f6 | |||
4c3565c618 |
137
.gitlab-ci.yml
137
.gitlab-ci.yml
@ -1,137 +0,0 @@
|
|||||||
# gitzone ci_default
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
|
|
||||||
cache:
|
|
||||||
paths:
|
|
||||||
- .npmci_cache/
|
|
||||||
key: '$CI_BUILD_STAGE'
|
|
||||||
|
|
||||||
stages:
|
|
||||||
- security
|
|
||||||
- test
|
|
||||||
- release
|
|
||||||
- metadata
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# security stage
|
|
||||||
# ====================
|
|
||||||
mirror:
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci git mirror
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
auditProductionDependencies:
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci command npm install --production --ignore-scripts
|
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
|
||||||
- npmci command npm audit --audit-level=high --only=prod --production
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
|
|
||||||
auditDevDependencies:
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci command npm install --ignore-scripts
|
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
|
||||||
- npmci command npm audit --audit-level=high --only=dev
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# test stage
|
|
||||||
# ====================
|
|
||||||
|
|
||||||
testStable:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
|
|
||||||
testBuild:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command npm run build
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
|
|
||||||
release:
|
|
||||||
stage: release
|
|
||||||
script:
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm publish
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# metadata stage
|
|
||||||
# ====================
|
|
||||||
codequality:
|
|
||||||
stage: metadata
|
|
||||||
allow_failure: true
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
script:
|
|
||||||
- npmci command npm install -g tslint typescript
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- priv
|
|
||||||
|
|
||||||
trigger:
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npmci trigger
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
pages:
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npmci node install lts
|
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command tsdoc
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
artifacts:
|
|
||||||
expire_in: 1 week
|
|
||||||
paths:
|
|
||||||
- public
|
|
||||||
allow_failure: true
|
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -2,28 +2,10 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "current file",
|
"command": "npm test",
|
||||||
"type": "node",
|
"name": "Run npm test",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"args": [
|
"type": "node-terminal"
|
||||||
"${relativeFile}"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "test.ts",
|
|
||||||
"type": "node",
|
|
||||||
"request": "launch",
|
|
||||||
"args": [
|
|
||||||
"test/test.ts"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
122
changelog.md
Normal file
122
changelog.md
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
# 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)
|
||||||
|
Remove .gitlab-ci.yml and update dependencies and metadata
|
||||||
|
|
||||||
|
- Removed .gitlab-ci.yml file
|
||||||
|
- Updated dependencies in package.json and npmextra.json
|
||||||
|
- Improved description and keywords for better package definition
|
||||||
|
|
||||||
|
## 2024-05-29 - 4.0.3 - Maintenance
|
||||||
|
Update project configuration and descriptions.
|
||||||
|
|
||||||
|
- Update project description
|
||||||
|
- Update tsconfig
|
||||||
|
- Update npmextra.json with githost field
|
||||||
|
|
||||||
|
## 2023-07-10 - 4.0.2 - Standards
|
||||||
|
Updates to adhere to new organizational standards.
|
||||||
|
|
||||||
|
- Switch to new org scheme
|
||||||
|
- Fix(core): update project settings
|
||||||
|
|
||||||
|
## 2023-04-17 - 4.0.0 - Major Release
|
||||||
|
Significant updates and switching to ESM.
|
||||||
|
|
||||||
|
- Fix(core): update project settings
|
||||||
|
|
||||||
|
## 2023-04-04 - 3.1.9 - Major Release
|
||||||
|
Breaking change: switch to ESM (ECMAScript Module).
|
||||||
|
|
||||||
|
## 2021-05-31 - 3.1.6 - Maintenance
|
||||||
|
Update project settings.
|
||||||
|
|
||||||
|
- Fix(core): update project settings
|
||||||
|
|
||||||
|
## 2021-04-23 - 3.1.4 - Maintenance
|
||||||
|
Update project settings.
|
||||||
|
|
||||||
|
- Fix(core): update project settings
|
||||||
|
|
||||||
|
## 2020-10-16 - 3.1.2 - Maintenance
|
||||||
|
Update project settings.
|
||||||
|
|
||||||
|
- Fix(core): update project settings
|
||||||
|
|
||||||
|
## 2020-10-15 - 3.0.9 - Feature
|
||||||
|
Added functionality for wrapping promises in timeouts.
|
||||||
|
|
||||||
|
- Feat(timeouts): allows the wrapping of promises in timeouts
|
||||||
|
|
||||||
|
## 2019-10-01 - 3.0.5 - Maintenance
|
||||||
|
Update project settings.
|
||||||
|
|
||||||
|
- Fix(core): update project settings
|
||||||
|
|
||||||
|
## 2019-03-26 - 3.0.0 - Major Release
|
||||||
|
Breaking change: remove util dependency.
|
||||||
|
|
||||||
|
- BREAKING CHANGE(remove util dependency and promisify functionality): update
|
||||||
|
|
||||||
|
## 2018-07-03 - 2.0.2 - Dependencies
|
||||||
|
Project now has zero dependencies.
|
||||||
|
|
||||||
|
- Fix(core): now has 0 dependencies
|
||||||
|
|
||||||
|
## 2018-03-16 - 1.1.8 - Major Release
|
||||||
|
Breaking change: switch scope to pushrocks.
|
||||||
|
|
||||||
|
- BREAKING CHANGE(scope): switch scope to pushrocks
|
||||||
|
|
||||||
|
## 2017-07-27 - 1.1.7 - Standards
|
||||||
|
Update to latest coding standards.
|
||||||
|
|
||||||
|
- Update to latest standards
|
||||||
|
|
||||||
|
## 2017-07-06 - 1.1.4 - Features and Improvements
|
||||||
|
Added polyfill and npmextra.json.
|
||||||
|
|
||||||
|
- Add polyfill
|
||||||
|
- Add npmextra.json
|
||||||
|
|
||||||
|
## 2017-07-06 - 1.1.2 - CI Maintenance
|
||||||
|
Updated continuous integration configuration.
|
||||||
|
|
||||||
|
- Update ci
|
@ -6,12 +6,26 @@
|
|||||||
"gitzone": {
|
"gitzone": {
|
||||||
"projectType": "npm",
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "code.foss.global",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "push.rocks",
|
||||||
"gitrepo": "smartpromise",
|
"gitrepo": "smartpromise",
|
||||||
"shortDescription": "simple promises and Deferred constructs",
|
"description": "A TypeScript library for managing promises and Deferred constructs, simplifying asynchronous programming.",
|
||||||
"npmPackagename": "@pushrocks/smartpromise",
|
"npmPackagename": "@push.rocks/smartpromise",
|
||||||
"license": "MIT"
|
"license": "MIT",
|
||||||
|
"keywords": [
|
||||||
|
"promise",
|
||||||
|
"deferred",
|
||||||
|
"async",
|
||||||
|
"promisify",
|
||||||
|
"cumulative deferred",
|
||||||
|
"timeout",
|
||||||
|
"typescript",
|
||||||
|
"asynchronous",
|
||||||
|
"utilities"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
25702
package-lock.json
generated
25702
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
43
package.json
43
package.json
@ -1,31 +1,31 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartpromise",
|
"name": "@push.rocks/smartpromise",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.1.6",
|
"version": "4.2.3",
|
||||||
"description": "simple promises and Deferred constructs",
|
"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)"
|
"build": "(tsbuild --web --allowimplicitany)",
|
||||||
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+ssh://git@gitlab.com/pushrocks/smartq.git"
|
"url": "https://code.foss.global/push.rocks/smartpromise.git"
|
||||||
},
|
},
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
"homepage": "https://code.foss.global/push.rocks/smartpromise",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@git.zone/tsbuild": "^2.2.0",
|
||||||
"@gitzone/tstest": "^1.0.54",
|
"@git.zone/tsrun": "^1.3.3",
|
||||||
"@pushrocks/tapbundle": "^3.2.14",
|
"@git.zone/tstest": "^1.0.92",
|
||||||
"@types/node": "^15.6.1",
|
"@push.rocks/tapbundle": "^5.5.6",
|
||||||
"tslint": "^6.1.3",
|
"@types/node": "^22.10.10"
|
||||||
"tslint-config-prettier": "^1.18.0"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@ -41,5 +41,24 @@
|
|||||||
],
|
],
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 chrome versions"
|
"last 1 chrome versions"
|
||||||
|
],
|
||||||
|
"type": "module",
|
||||||
|
"keywords": [
|
||||||
|
"promise",
|
||||||
|
"deferred",
|
||||||
|
"async",
|
||||||
|
"promisify",
|
||||||
|
"cumulative deferred",
|
||||||
|
"timeout",
|
||||||
|
"typescript",
|
||||||
|
"asynchronous",
|
||||||
|
"utilities"
|
||||||
|
],
|
||||||
|
"pnpm": {
|
||||||
|
"onlyBuiltDependencies": [
|
||||||
|
"esbuild",
|
||||||
|
"mongodb-memory-server",
|
||||||
|
"puppeteer"
|
||||||
]
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
9697
pnpm-lock.yaml
generated
Normal file
9697
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
379
readme.md
379
readme.md
@ -1,98 +1,311 @@
|
|||||||
# @pushrocks/smartpromise
|
# @push.rocks/smartpromise
|
||||||
simple promises and Deferred constructs
|
|
||||||
|
|
||||||
## Availabililty and Links
|
A library for simple promises and Deferred constructs with TypeScript support.
|
||||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpromise)
|
|
||||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartpromise)
|
|
||||||
* [github.com (source mirror)](https://github.com/pushrocks/smartpromise)
|
|
||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartpromise/)
|
|
||||||
|
|
||||||
## Status for master
|
## Install
|
||||||
|
|
||||||
Status Category | Status Badge
|
```bash
|
||||||
-- | --
|
npm install @push.rocks/smartpromise --save
|
||||||
GitLab Pipelines | [](https://lossless.cloud)
|
```
|
||||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
|
||||||
npm | [](https://lossless.cloud)
|
This module is designed to be used with TypeScript for the best developer experience, providing type safety and IntelliSense in your IDE.
|
||||||
Snyk | [](https://lossless.cloud)
|
|
||||||
TypeScript Support | [](https://lossless.cloud)
|
|
||||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
Code Style | [](https://lossless.cloud)
|
|
||||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
|
||||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
|
||||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
|
||||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
`@push.rocks/smartpromise` simplifies the use of promises and deferred constructs in TypeScript, offering a set of utility functions that extend native Promise capabilities in specific scenarios. This guide walks you through its functionalities, showcasing how to leverage this library in a TypeScript project.
|
||||||
|
|
||||||
> Note: smartq uses native ES6 promises
|
### Setting Up Your Project
|
||||||
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
|
|
||||||
|
|
||||||
```typescript
|
Ensure your TypeScript project is configured to support ES Module syntax. In your `tsconfig.json`, you should have:
|
||||||
import * as q from '@pushrocks/smartpromise';
|
|
||||||
|
|
||||||
// Deferred
|
```json
|
||||||
// -----------------------------------------------
|
{
|
||||||
let myAsyncFunction = (): Promise<string> => {
|
"compilerOptions": {
|
||||||
let done = q.defer<string>(); // returns your typical Deferred object
|
"module": "ESNext",
|
||||||
setTimeout(() => {
|
"target": "ESNext",
|
||||||
done.resolve('hi'); // will throw type error for other types than string as argument ;)
|
"moduleResolution": "node"
|
||||||
}, 6000);
|
}
|
||||||
|
}
|
||||||
console.log(done.status); // logs "pending";
|
|
||||||
done.promise.then(() => {
|
|
||||||
console.log(done.status); // logs "fullfilled"
|
|
||||||
console.log(done.duration); // logs the milliseconds between instantiation and fullfillment
|
|
||||||
});
|
|
||||||
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
let myAsyncFunction2 = async () => {
|
|
||||||
let aString = await myAsyncFunction();
|
|
||||||
console.log(aString); // will log 'hi' to console
|
|
||||||
};
|
|
||||||
|
|
||||||
myAsyncFunction2();
|
|
||||||
|
|
||||||
// Resolved and Rejected promises
|
|
||||||
// ------------------------------------------------
|
|
||||||
q.resolvedPromise(`I'll get logged to console soon`).then((x) => {
|
|
||||||
console.log(x);
|
|
||||||
});
|
|
||||||
|
|
||||||
q.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);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Promisify (typed)
|
|
||||||
// ------------------------------------------------
|
|
||||||
|
|
||||||
let myCallbackedFunction = (someString: string, someNumber: number, cb) => {
|
|
||||||
cb(null, someString);
|
|
||||||
};
|
|
||||||
|
|
||||||
let myPromisedFunction = q.promisify(myCallbackFunction);
|
|
||||||
myPromisedFunction('helloThere', 2).then((x) => {
|
|
||||||
console.log(x); // will log 'helloThere' to console
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Contribution
|
### Basic Usage
|
||||||
|
|
||||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
#### Creating a Deferred
|
||||||
|
|
||||||
For further information read the linked docs at the top of this readme.
|
Deferred objects in `@push.rocks/smartpromise` give you more control over your promises, allowing for manual resolution or rejection beyond the initial executor function.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
```typescript
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
import { defer } from '@push.rocks/smartpromise';
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
async function exampleDeferred(): Promise<string> {
|
||||||
|
const myDeferred = defer<string>();
|
||||||
|
|
||||||
|
// Simulate an async task
|
||||||
|
setTimeout(() => {
|
||||||
|
myDeferred.resolve('Hello, Deferred!');
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
return myDeferred.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
exampleDeferred().then((result) => console.log(result));
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Cumulative Deferred
|
||||||
|
|
||||||
|
For scenarios where multiple asynchronous tasks need to be tracked collectively before proceeding, use `CumulativeDeferred`. It waits for all added promises to resolve.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { cumulativeDefer } from '@push.rocks/smartpromise';
|
||||||
|
|
||||||
|
async function exampleCumulativeDeferred() {
|
||||||
|
const myCumulativeDeferred = cumulativeDefer();
|
||||||
|
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
myCumulativeDeferred.addPromise(new Promise((resolve) => setTimeout(resolve, 1000 * i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
await myCumulativeDeferred.promise;
|
||||||
|
console.log('All tasks completed!');
|
||||||
|
}
|
||||||
|
|
||||||
|
exampleCumulativeDeferred();
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Utilizing Resolved and Rejected Promises
|
||||||
|
|
||||||
|
Quickly create already resolved or rejected promises for testing or initialization purposes.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { resolvedPromise, rejectedPromise } from '@push.rocks/smartpromise';
|
||||||
|
|
||||||
|
resolvedPromise('immediately resolved').then(console.log);
|
||||||
|
rejectedPromise('immediately rejected').catch(console.error);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Advanced Use Cases
|
||||||
|
|
||||||
|
#### Promisify Callback Functions
|
||||||
|
|
||||||
|
`@push.rocks/smartpromise` does not directly provide a `promisify` function like Node.js `util` module, but you can easily integrate existing functions or use third-party libraries to convert callback-based functions into promises.
|
||||||
|
|
||||||
|
#### Handling Timeouts and Continuations
|
||||||
|
|
||||||
|
Managing timeouts or long-running promises gets easier with helper functions.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { timeoutWrap, timeoutAndContinue } from '@push.rocks/smartpromise';
|
||||||
|
|
||||||
|
async function exampleTimeout() {
|
||||||
|
const myPromise = new Promise((resolve) => setTimeout(() => resolve('Done!'), 2000));
|
||||||
|
|
||||||
|
// Will reject if the promise does not resolve within 1 second
|
||||||
|
try {
|
||||||
|
const result = await timeoutWrap(myPromise, 1000);
|
||||||
|
console.log(result);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Promise timed out');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Continues after 1 second, regardless of whether the promise has resolved
|
||||||
|
const result = await timeoutAndContinue(myPromise, 1000);
|
||||||
|
console.log(result); // May log `null` if the original promise did not resolve in time
|
||||||
|
}
|
||||||
|
|
||||||
|
exampleTimeout();
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Map and Reduce Asynchronous Functions
|
||||||
|
|
||||||
|
Suppose you have a collection of items that you need to process asynchronously. You can use the `map` function to apply an asynchronous function to each item in the array and wait for all the promises to resolve.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { map } from '@push.rocks/smartpromise';
|
||||||
|
|
||||||
|
async function processData(items: string[]): Promise<string[]> {
|
||||||
|
return map(items, async (item) => {
|
||||||
|
// Simulate an async operation
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||||
|
return item.toUpperCase();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
processData(['hello', 'world']).then(console.log);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Handling Complex Promise Scenarios
|
||||||
|
|
||||||
|
#### Managing Multiple Deferreds
|
||||||
|
|
||||||
|
If you have multiple deferred objects and you want to manage them collectively, you can do so using the `CumulativeDeferred` class. This is especially useful when you have a dynamic number of deferreds to resolve.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { defer, cumulativeDefer } from '@push.rocks/smartpromise';
|
||||||
|
|
||||||
|
async function exampleComplexDeferred(): Promise<void> {
|
||||||
|
const task1 = defer<string>();
|
||||||
|
const task2 = defer<string>();
|
||||||
|
const cumulative = cumulativeDefer();
|
||||||
|
|
||||||
|
cumulative.addPromise(task1.promise);
|
||||||
|
cumulative.addPromise(task2.promise);
|
||||||
|
|
||||||
|
// Simulate async tasks
|
||||||
|
setTimeout(() => task1.resolve('Task 1 complete'), 1000);
|
||||||
|
setTimeout(() => task2.resolve('Task 2 complete'), 2000);
|
||||||
|
|
||||||
|
await cumulative.promise;
|
||||||
|
|
||||||
|
console.log('All tasks completed');
|
||||||
|
}
|
||||||
|
|
||||||
|
exampleComplexDeferred();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Other Utilities
|
||||||
|
|
||||||
|
#### Race Condition Handling with `getFirstTrueOrFalse`
|
||||||
|
|
||||||
|
This helper function resolves immediately when one of the provided promises resolves to `true` or when all promises are resolved to `false`.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { getFirstTrueOrFalse } from '@push.rocks/smartpromise';
|
||||||
|
|
||||||
|
async function exampleRaceCondition() {
|
||||||
|
const task1 = new Promise<boolean>((resolve) => setTimeout(() => resolve(true), 1000));
|
||||||
|
const task2 = new Promise<boolean>((resolve) => setTimeout(() => resolve(false), 2000));
|
||||||
|
|
||||||
|
const result = await getFirstTrueOrFalse([task1, task2]);
|
||||||
|
console.log(result); // Outputs: true
|
||||||
|
}
|
||||||
|
|
||||||
|
exampleRaceCondition();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Complete Use Case
|
||||||
|
|
||||||
|
Let's create a comprehensive example that showcases multiple features of `@push.rocks/smartpromise`.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import {
|
||||||
|
defer,
|
||||||
|
cumulativeDefer,
|
||||||
|
resolvedPromise,
|
||||||
|
rejectedPromise,
|
||||||
|
timeoutWrap,
|
||||||
|
timeoutAndContinue,
|
||||||
|
map,
|
||||||
|
getFirstTrueOrFalse,
|
||||||
|
} from '@push.rocks/smartpromise';
|
||||||
|
|
||||||
|
async function completeUseCaseExample() {
|
||||||
|
console.log('Starting Complete Use Case Example!');
|
||||||
|
|
||||||
|
// Using Deferred
|
||||||
|
const myDeferred = defer<string>();
|
||||||
|
setTimeout(() => myDeferred.resolve('Deferred Resolved!'), 500);
|
||||||
|
console.log(await myDeferred.promise); // Outputs: "Deferred Resolved!"
|
||||||
|
|
||||||
|
// Using Cumulative Deferred
|
||||||
|
const myCumulativeDeferred = cumulativeDefer();
|
||||||
|
myCumulativeDeferred.addPromise(new Promise((resolve) => setTimeout(() => resolve('Task 1'), 1000)));
|
||||||
|
myCumulativeDeferred.addPromise(new Promise((resolve) => setTimeout(() => resolve('Task 2'), 2000)));
|
||||||
|
await myCumulativeDeferred.promise;
|
||||||
|
console.log('All cumulative tasks completed');
|
||||||
|
|
||||||
|
// Using Resolved and Rejected Promises
|
||||||
|
await resolvedPromise('Instant Resolve').then(console.log); // Outputs: "Instant Resolve"
|
||||||
|
await rejectedPromise('Instant Reject').catch(console.error); // Outputs: "Instant Reject"
|
||||||
|
|
||||||
|
// Using timeoutWrap
|
||||||
|
try {
|
||||||
|
const delayedPromise = new Promise((resolve) => setTimeout(() => resolve('Finished'), 3000));
|
||||||
|
const result = await timeoutWrap(delayedPromise, 1000);
|
||||||
|
console.log(result);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Timeout occurred'); // Outputs: "Timeout occurred"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Using timeoutAndContinue
|
||||||
|
const resultContinue = await timeoutAndContinue(
|
||||||
|
new Promise((resolve) => setTimeout(() => resolve('Finished eventually'), 3000)),
|
||||||
|
1000
|
||||||
|
);
|
||||||
|
console.log(resultContinue); // Outputs: null (since it didn't resolve in 1 second)
|
||||||
|
|
||||||
|
// Using Map
|
||||||
|
const items = ['a', 'b', 'c'];
|
||||||
|
const processedItems = await map(items, async (item) => {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||||
|
return item.toUpperCase();
|
||||||
|
});
|
||||||
|
console.log(processedItems); // Outputs: ['A', 'B', 'C']
|
||||||
|
|
||||||
|
// Using getFirstTrueOrFalse
|
||||||
|
const raceResults = await getFirstTrueOrFalse([
|
||||||
|
new Promise<boolean>((resolve) => setTimeout(() => resolve(false), 1000)),
|
||||||
|
new Promise<boolean>((resolve) => setTimeout(() => resolve(true), 2000)),
|
||||||
|
]);
|
||||||
|
console.log(raceResults); // Outputs: true
|
||||||
|
|
||||||
|
console.log('Complete Use Case Example Finished!');
|
||||||
|
}
|
||||||
|
|
||||||
|
completeUseCaseExample();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Testing Your Code
|
||||||
|
|
||||||
|
Testing is crucial to ensure the reliability of your asynchronous workflows. You can write tests using the `@push.rocks/tapbundle` library to create unit tests for your promises and deferred constructs.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { tap, expect } from '@push.rocks/tapbundle';
|
||||||
|
import * as smartpromise from '@push.rocks/smartpromise';
|
||||||
|
|
||||||
|
tap.test('should resolve a deferred promise', async () => {
|
||||||
|
const deferred = smartpromise.defer<string>();
|
||||||
|
deferred.resolve('Resolved!');
|
||||||
|
const result = await deferred.promise;
|
||||||
|
expect(result).toEqual('Resolved!');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should timeout a long-running promise', async () => {
|
||||||
|
const longRunningPromise = new Promise((resolve) => setTimeout(resolve, 2000));
|
||||||
|
try {
|
||||||
|
await smartpromise.timeoutWrap(longRunningPromise, 1000);
|
||||||
|
} catch (err) {
|
||||||
|
expect(err.message).toEqual('timeout');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should map async function to an array', async () => {
|
||||||
|
const inputArray = ['a', 'b'];
|
||||||
|
const result = await smartpromise.map(inputArray, async (item) => item.toUpperCase());
|
||||||
|
expect(result).toEqual(['A', 'B']);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
||||||
|
```
|
||||||
|
|
||||||
|
By following this guide and using the examples provided, you should be able to effectively use `@push.rocks/smartpromise` for managing promises and deferred constructs in your TypeScript project. The library's extensive utility functions, combined with TypeScript support, make it a powerful tool for modern asynchronous programming needs.
|
||||||
|
|
||||||
|
Explore the full range of features and feel free to read through the source code to learn more about the implementation details. Happy coding!
|
||||||
|
|
||||||
|
## License and Legal Information
|
||||||
|
|
||||||
|
This 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.
|
||||||
|
|
||||||
|
**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.
|
||||||
|
|
||||||
|
### Trademarks
|
||||||
|
|
||||||
|
This 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.
|
||||||
|
|
||||||
|
### Company Information
|
||||||
|
|
||||||
|
Task Venture Capital GmbH
|
||||||
|
Registered at District court Bremen HRB 35230 HB, Germany
|
||||||
|
|
||||||
|
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||||
|
|
||||||
|
By 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.
|
||||||
|
@ -1,19 +1,18 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@push.rocks/tapbundle';
|
||||||
import * as smartpromise from '../ts/index';
|
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 () => {
|
||||||
const myDeferred = smartpromise.defer();
|
const myDeferred = smartpromise.defer();
|
||||||
const expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
|
|
||||||
myDeferred.resolve();
|
myDeferred.resolve();
|
||||||
return expectPromise;
|
await myDeferred.promise;
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should let types flow through the Promise', async () => {
|
tap.test('should let types flow through the Promise', async () => {
|
||||||
const myString = 'someString';
|
const myString = 'someString';
|
||||||
const myDeferred = smartpromise.defer<string>();
|
const myDeferred = smartpromise.defer<string>();
|
||||||
const expectPromise = expect(myDeferred.promise).to.eventually.equal('someString');
|
|
||||||
myDeferred.resolve(myString);
|
myDeferred.resolve(myString);
|
||||||
return expectPromise;
|
const result = await myDeferred.promise;
|
||||||
|
expect(result).toEqual('someString');
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should map callbacks', async () => {
|
tap.test('should map callbacks', async () => {
|
||||||
@ -21,10 +20,8 @@ tap.test('should map callbacks', async () => {
|
|||||||
const myPromisified = async (myInput) => {
|
const myPromisified = async (myInput) => {
|
||||||
return myInput;
|
return myInput;
|
||||||
};
|
};
|
||||||
const expectPromise = expect(
|
const result = await smartpromise.map(inputArray, myPromisified);
|
||||||
smartpromise.map(inputArray, myPromisified)
|
expect(result).toEqual(inputArray);
|
||||||
).to.eventually.deep.equal(inputArray);
|
|
||||||
return expectPromise;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
export default tap.start();
|
||||||
|
@ -1,54 +1,57 @@
|
|||||||
import * as smartpromise from '../ts';
|
import * as smartpromise from '../ts/index.js';
|
||||||
|
import { tap, expect } from '@push.rocks/tapbundle';
|
||||||
|
|
||||||
// using Deferreds
|
tap.test('simple deferred should resolve with correct value', async () => {
|
||||||
// simple deferred;
|
const done = smartpromise.defer<string>();
|
||||||
const done = smartpromise.defer();
|
let result: string | undefined;
|
||||||
done.promise.then((stringArg) => {
|
|
||||||
console.log(stringArg);
|
|
||||||
});
|
|
||||||
done.resolve('hello') // whenever you are ready
|
|
||||||
|
|
||||||
|
done.promise.then((stringArg) => {
|
||||||
|
result = stringArg;
|
||||||
|
});
|
||||||
|
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 '@pushrocks/tapbundle';
|
tap.test('rejectedPromise should reject with correct error message', async () => {
|
||||||
|
const errorMessage = 'what a lovely error message';
|
||||||
|
let caught = false;
|
||||||
|
|
||||||
tap.test('runs through', async () => {})
|
try {
|
||||||
tap.start();
|
await smartpromise.rejectedPromise(errorMessage);
|
||||||
|
} catch (err) {
|
||||||
|
caught = true;
|
||||||
|
expect(err).toEqual(errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(caught).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default tap.start();
|
||||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @push.rocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@push.rocks/smartpromise',
|
||||||
|
version: '4.2.3',
|
||||||
|
description: 'A TypeScript library for managing promises and Deferred constructs, simplifying asynchronous programming.'
|
||||||
|
}
|
86
ts/index.ts
86
ts/index.ts
@ -1,50 +1,8 @@
|
|||||||
export interface IResolve<T> {
|
import { defer } from './smartpromise.classes.deferred.js';
|
||||||
(value?: T | PromiseLike<T>): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IReject {
|
export * from './smartpromise.classes.cumulativedeferred.js';
|
||||||
(reason?: any): void;
|
export * from './smartpromise.classes.deferred.js';
|
||||||
}
|
|
||||||
|
|
||||||
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
|
|
||||||
|
|
||||||
export class Deferred<T> {
|
|
||||||
public promise: Promise<T>;
|
|
||||||
public resolve: IResolve<T>;
|
|
||||||
public reject: IReject;
|
|
||||||
public status: TDeferredStatus;
|
|
||||||
|
|
||||||
public startedAt: number;
|
|
||||||
public stoppedAt: number;
|
|
||||||
public get duration(): number {
|
|
||||||
if (this.stoppedAt) {
|
|
||||||
return this.stoppedAt - this.startedAt;
|
|
||||||
} else {
|
|
||||||
return Date.now() - this.startedAt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.promise = new Promise<T>((resolve, reject) => {
|
|
||||||
this.resolve = (valueArg: T | PromiseLike<T>) => {
|
|
||||||
this.status = 'fulfilled';
|
|
||||||
this.stoppedAt = Date.now();
|
|
||||||
resolve(valueArg);
|
|
||||||
};
|
|
||||||
this.reject = (reason: any) => {
|
|
||||||
this.status = 'rejected';
|
|
||||||
this.stoppedAt = Date.now();
|
|
||||||
reject(reason);
|
|
||||||
};
|
|
||||||
this.startedAt = Date.now();
|
|
||||||
this.status = 'pending';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const defer = <T>() => {
|
|
||||||
return new Deferred<T>();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new resolved promise for the provided value.
|
* Creates a new resolved promise for the provided value.
|
||||||
@ -84,15 +42,30 @@ export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
|||||||
return resultArray;
|
return resultArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const timeoutWrap = <T = any>(promiseArg: Promise<T>, timeoutInMs: number) => {
|
export const timeoutWrap = async <T = any>(
|
||||||
|
promiseArg: Promise<T>,
|
||||||
|
timeoutInMsArg: number,
|
||||||
|
rejectArg = true
|
||||||
|
) => {
|
||||||
return new Promise<T>((resolve, reject) => {
|
return new Promise<T>((resolve, reject) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
if (rejectArg) {
|
||||||
reject(new Error('timeout'));
|
reject(new Error('timeout'));
|
||||||
}, timeoutInMs);
|
} else {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
}, timeoutInMsArg);
|
||||||
promiseArg.then(resolve, reject);
|
promiseArg.then(resolve, reject);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const timeoutAndContinue = async <T = any>(
|
||||||
|
promiseArg: Promise<T>,
|
||||||
|
timeoutInMsArg = 60000
|
||||||
|
) => {
|
||||||
|
return timeoutWrap(promiseArg, timeoutInMsArg, false);
|
||||||
|
};
|
||||||
|
|
||||||
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
|
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
|
||||||
const done = defer<boolean>();
|
const done = defer<boolean>();
|
||||||
for (const promiseArg of promisesArg) {
|
for (const promiseArg of promisesArg) {
|
||||||
@ -107,3 +80,22 @@ 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
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
32
ts/smartpromise.classes.cumulativedeferred.ts
Normal file
32
ts/smartpromise.classes.cumulativedeferred.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { defer } from "./smartpromise.classes.deferred.js";
|
||||||
|
|
||||||
|
export class CumulativeDeferred {
|
||||||
|
private accumulatedPromises: Promise<any>[] = [];
|
||||||
|
private deferred = defer();
|
||||||
|
public promise = this.deferred.promise;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
setTimeout(async () => {
|
||||||
|
while (this.accumulatedPromises.length > 0) {
|
||||||
|
const poppedPromise = this.accumulatedPromises.shift();
|
||||||
|
await poppedPromise;
|
||||||
|
}
|
||||||
|
this.deferred.resolve();
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public subDefer() {
|
||||||
|
const done = defer();
|
||||||
|
this.addPromise(done.promise);
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
public addPromise(promiseArg: Promise<any>) {
|
||||||
|
this.accumulatedPromises.push(promiseArg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const cumulativeDefer = () => {
|
||||||
|
return new CumulativeDeferred();
|
||||||
|
}
|
54
ts/smartpromise.classes.deferred.ts
Normal file
54
ts/smartpromise.classes.deferred.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
export interface IResolve<T> {
|
||||||
|
(value?: T | PromiseLike<T>): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IReject {
|
||||||
|
(reason?: any): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
|
||||||
|
|
||||||
|
export class Deferred<T> {
|
||||||
|
public promise: Promise<T>;
|
||||||
|
public resolve: IResolve<T>;
|
||||||
|
public reject: IReject;
|
||||||
|
public status: TDeferredStatus;
|
||||||
|
public claimed = false;
|
||||||
|
public claim() {
|
||||||
|
if (this.claimed) {
|
||||||
|
throw new Error('Deferred already claimed');
|
||||||
|
}
|
||||||
|
this.claimed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public startedAt: number;
|
||||||
|
public stoppedAt: number;
|
||||||
|
public get duration(): number {
|
||||||
|
if (this.stoppedAt) {
|
||||||
|
return this.stoppedAt - this.startedAt;
|
||||||
|
} else {
|
||||||
|
return Date.now() - this.startedAt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.promise = new Promise<T>((resolve, reject) => {
|
||||||
|
this.resolve = (valueArg: T | PromiseLike<T>) => {
|
||||||
|
this.status = 'fulfilled';
|
||||||
|
this.stoppedAt = Date.now();
|
||||||
|
resolve(valueArg);
|
||||||
|
};
|
||||||
|
this.reject = (reason: any) => {
|
||||||
|
this.status = 'rejected';
|
||||||
|
this.stoppedAt = Date.now();
|
||||||
|
reject(reason);
|
||||||
|
};
|
||||||
|
this.startedAt = Date.now();
|
||||||
|
this.status = 'pending';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const defer = <T>() => {
|
||||||
|
return new Deferred<T>();
|
||||||
|
};
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"verbatimModuleSyntax": true
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"dist_*/**/*.d.ts"
|
||||||
|
]
|
||||||
|
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user