Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
91b52982c0 | |||
97f8327fe9 | |||
42f40449ad | |||
960c23fecd | |||
40642fd6f6 | |||
4c3565c618 | |||
1848006601 | |||
c761cc4460 | |||
9f6d88e162 | |||
255d6415a5 | |||
84a8d8431c | |||
95d27d52a7 | |||
1363aa7957 | |||
095ea36e5d | |||
b6ab079de0 | |||
734d5aca35 | |||
3b0052602f | |||
9693ec04d6 | |||
4ee932873c | |||
3bedd3581e |
@ -12,40 +12,36 @@ stages:
|
|||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- pnpm install -g pnpm
|
||||||
|
- pnpm install -g @shipzone/npmci
|
||||||
|
- npmci npm prepare
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# security stage
|
||||||
|
# ====================
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
mirror:
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci git mirror
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
auditProductionDependencies:
|
auditProductionDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
|
||||||
- npmci command npm install --production --ignore-scripts
|
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command npm audit --audit-level=high --only=prod --production
|
- npmci command pnpm audit --audit-level=high --prod
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
auditDevDependencies:
|
auditDevDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
|
||||||
- npmci command npm install --ignore-scripts
|
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command npm audit --audit-level=high --only=dev
|
- npmci command pnpm audit --audit-level=high --dev
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
|
||||||
@ -56,7 +52,6 @@ auditDevDependencies:
|
|||||||
testStable:
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
@ -67,10 +62,9 @@ testStable:
|
|||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command npm run build
|
- npmci npm build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
@ -96,10 +90,9 @@ codequality:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
@ -119,11 +112,9 @@ trigger:
|
|||||||
pages:
|
pages:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command tsdoc
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
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"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
"gitrepo": "smartpromise",
|
"gitrepo": "smartpromise",
|
||||||
"shortDescription": "simple promises and Deferred constructs",
|
"description": "simple promises and Deferred constructs",
|
||||||
"npmPackagename": "@pushrocks/smartpromise",
|
"npmPackagename": "@pushrocks/smartpromise",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
11064
package-lock.json
generated
11064
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@ -1,13 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartpromise",
|
"name": "@pushrocks/smartpromise",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.0.9",
|
"version": "3.1.9",
|
||||||
"description": "simple promises and Deferred constructs",
|
"description": "simple promises and Deferred constructs",
|
||||||
"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/)",
|
"test": "(tstest test/ --web)",
|
||||||
"build": "(tsbuild)"
|
"build": "(tsbuild --web --allowimplicitany)",
|
||||||
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -19,14 +20,12 @@
|
|||||||
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
||||||
"dependencies": {},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@gitzone/tsbuild": "^2.1.65",
|
||||||
"@gitzone/tstest": "^1.0.52",
|
"@gitzone/tsrun": "^1.2.39",
|
||||||
"@pushrocks/tapbundle": "^3.2.9",
|
"@gitzone/tstest": "^1.0.74",
|
||||||
"@types/node": "^14.11.8",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"tslint": "^6.1.3",
|
"@types/node": "^18.15.11"
|
||||||
"tslint-config-prettier": "^1.18.0"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@ -42,5 +41,6 @@
|
|||||||
],
|
],
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 chrome versions"
|
"last 1 chrome versions"
|
||||||
]
|
],
|
||||||
|
"type": "module"
|
||||||
}
|
}
|
||||||
|
4460
pnpm-lock.yaml
generated
Normal file
4460
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
96
readme.md
Normal file
96
readme.md
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
# @pushrocks/smartpromise
|
||||||
|
simple promises and Deferred constructs
|
||||||
|
|
||||||
|
## Availabililty and Links
|
||||||
|
* [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
|
||||||
|
|
||||||
|
Status Category | Status Badge
|
||||||
|
-- | --
|
||||||
|
GitLab Pipelines | [](https://lossless.cloud)
|
||||||
|
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||||
|
npm | [](https://lossless.cloud)
|
||||||
|
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)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
|
> Note: smartq uses native ES6 promises
|
||||||
|
> smartq does not repeat any native functions, so for things like .all() simply use Promise.all()
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import * as q from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
|
// Deferred
|
||||||
|
// -----------------------------------------------
|
||||||
|
let myAsyncFunction = (): Promise<string> => {
|
||||||
|
let done = q.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";
|
||||||
|
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
|
||||||
|
|
||||||
|
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). :)
|
||||||
|
|
||||||
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
|
## Legal
|
||||||
|
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||||
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
@ -1,19 +1,18 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/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();
|
tap.start();
|
53
test/test.readme.node.ts
Normal file
53
test/test.readme.node.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
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 '@pushrocks/tapbundle';
|
||||||
|
|
||||||
|
tap.test('runs through', async () => {});
|
||||||
|
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 @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/smartpromise',
|
||||||
|
version: '3.1.9',
|
||||||
|
description: 'simple promises and Deferred constructs'
|
||||||
|
}
|
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,11 +42,41 @@ export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
|||||||
return resultArray;
|
return resultArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const timeoutWrap = (ms, promise) => {
|
export const timeoutWrap = async <T = any>(
|
||||||
return new Promise((resolve, reject) => {
|
promiseArg: Promise<T>,
|
||||||
|
timeoutInMsArg: number,
|
||||||
|
rejectArg = true
|
||||||
|
) => {
|
||||||
|
return new Promise<T>((resolve, reject) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
if (rejectArg) {
|
||||||
reject(new Error('timeout'));
|
reject(new Error('timeout'));
|
||||||
}, ms);
|
} else {
|
||||||
promise.then(resolve, reject);
|
resolve(null);
|
||||||
|
}
|
||||||
|
}, timeoutInMsArg);
|
||||||
|
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>[]) => {
|
||||||
|
const done = defer<boolean>();
|
||||||
|
for (const promiseArg of promisesArg) {
|
||||||
|
promiseArg.then((resultArg) => {
|
||||||
|
if (resultArg === true) {
|
||||||
|
done.resolve(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Promise.all(promisesArg).then(() => {
|
||||||
|
done.resolve(false);
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
};
|
||||||
|
26
ts/smartpromise.classes.cumulativedeferred.ts
Normal file
26
ts/smartpromise.classes.cumulativedeferred.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
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 addPromise(promiseArg: Promise<any>) {
|
||||||
|
this.accumulatedPromises.push(promiseArg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const cumulativeDefer = () => {
|
||||||
|
return new CumulativeDeferred();
|
||||||
|
}
|
47
ts/smartpromise.classes.deferred.ts
Normal file
47
ts/smartpromise.classes.deferred.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
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 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>();
|
||||||
|
};
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
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"
|
|
||||||
}
|
|
Reference in New Issue
Block a user