Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
91b52982c0 | |||
97f8327fe9 | |||
42f40449ad | |||
960c23fecd | |||
40642fd6f6 | |||
4c3565c618 | |||
1848006601 | |||
c761cc4460 | |||
9f6d88e162 | |||
255d6415a5 | |||
84a8d8431c | |||
95d27d52a7 | |||
1363aa7957 | |||
095ea36e5d | |||
b6ab079de0 | |||
734d5aca35 | |||
3b0052602f | |||
9693ec04d6 | |||
4ee932873c | |||
3bedd3581e | |||
ee3c1c6e79 | |||
8c6ac181b0 | |||
51bce0b317 | |||
374d73f87e | |||
d922c691ed | |||
5aecc44ad7 | |||
f7492c4656 | |||
63dd46ed49 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -15,8 +15,6 @@ node_modules/
|
|||||||
|
|
||||||
# builds
|
# builds
|
||||||
dist/
|
dist/
|
||||||
dist_web/
|
dist_*/
|
||||||
dist_serve/
|
|
||||||
dist_ts_web/
|
|
||||||
|
|
||||||
# custom
|
# custom
|
@ -4,7 +4,7 @@ image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
@ -12,27 +12,38 @@ stages:
|
|||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- pnpm install -g pnpm
|
||||||
|
- pnpm install -g @shipzone/npmci
|
||||||
|
- npmci npm prepare
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
mirror:
|
# ====================
|
||||||
|
# security stage
|
||||||
|
# ====================
|
||||||
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command pnpm audit --audit-level=high --prod
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
snyk:
|
auditDevDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command npm install -g snyk
|
- npmci command pnpm audit --audit-level=high --dev
|
||||||
- npmci command npm install --ignore-scripts
|
|
||||||
- npmci command snyk test
|
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
@ -41,26 +52,22 @@ snyk:
|
|||||||
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
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- priv
|
|
||||||
|
|
||||||
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
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@ -70,6 +77,7 @@ release:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
@ -79,11 +87,14 @@ release:
|
|||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g typescript
|
||||||
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- priv
|
||||||
|
|
||||||
@ -94,20 +105,18 @@ trigger:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-dbase:npmci
|
|
||||||
services:
|
|
||||||
- docker:stable-dind
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
- npmci node install stable
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command tsdoc
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"command": "npm test",
|
||||||
|
"name": "Run npm test",
|
||||||
|
"request": "launch",
|
||||||
|
"type": "node-terminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
90
README.md
90
README.md
@ -1,90 +0,0 @@
|
|||||||
# @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
|
|
||||||
|
|
||||||
[](https://gitlab.com/pushrocks/smartpromise/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/smartpromise/commits/master)
|
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartpromise)
|
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smartpromise)
|
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
[](https://prettier.io/)
|
|
||||||
|
|
||||||
## 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
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
For further information read the linked docs at the top of this readme.
|
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
||||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
|
@ -4,11 +4,12 @@
|
|||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
},
|
},
|
||||||
"gitzone": {
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"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"
|
||||||
}
|
}
|
||||||
|
1718
package-lock.json
generated
1718
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
42
package.json
42
package.json
@ -1,13 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartpromise",
|
"name": "@pushrocks/smartpromise",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.0.5",
|
"version": "3.1.9",
|
||||||
"description": "simple promises and Deferred constructs",
|
"description": "simple promises and Deferred constructs",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/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,24 +20,27 @@
|
|||||||
"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.17",
|
"@gitzone/tsbuild": "^2.1.65",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tsrun": "^1.2.39",
|
||||||
"@pushrocks/tapbundle": "^3.0.13",
|
"@gitzone/tstest": "^1.0.74",
|
||||||
"@types/node": "^12.7.5",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"tslint": "^5.20.0",
|
"@types/node": "^18.15.11"
|
||||||
"tslint-config-prettier": "^1.18.0"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/*",
|
"ts/**/*",
|
||||||
"ts_web/*",
|
"ts_web/**/*",
|
||||||
"dist/*",
|
"dist/**/*",
|
||||||
"dist_web/*",
|
"dist_*/**/*",
|
||||||
"dist_ts_web/*",
|
"dist_ts/**/*",
|
||||||
"assets/*",
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
]
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"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)
|
27
test/test.both.ts
Normal file
27
test/test.both.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
|
import * as smartpromise from '../ts/index.js';
|
||||||
|
|
||||||
|
tap.test('should return a Deferred for .defer()', async () => {
|
||||||
|
const myDeferred = smartpromise.defer();
|
||||||
|
myDeferred.resolve();
|
||||||
|
await myDeferred.promise;
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should let types flow through the Promise', async () => {
|
||||||
|
const myString = 'someString';
|
||||||
|
const myDeferred = smartpromise.defer<string>();
|
||||||
|
myDeferred.resolve(myString);
|
||||||
|
const result = await myDeferred.promise;
|
||||||
|
expect(result).toEqual('someString');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should map callbacks', async () => {
|
||||||
|
const inputArray = ['hi', 'awesome'];
|
||||||
|
const myPromisified = async (myInput) => {
|
||||||
|
return myInput;
|
||||||
|
};
|
||||||
|
const result = await smartpromise.map(inputArray, myPromisified);
|
||||||
|
expect(result).toEqual(inputArray);
|
||||||
|
});
|
||||||
|
|
||||||
|
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();
|
30
test/test.ts
30
test/test.ts
@ -1,30 +0,0 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
|
||||||
import * as q from '../ts/index';
|
|
||||||
|
|
||||||
tap.test('should return a Deferred for .defer()', async () => {
|
|
||||||
let myDeferred = q.defer();
|
|
||||||
let expectPromise = expect(myDeferred.promise).to.eventually.be.fulfilled;
|
|
||||||
myDeferred.resolve();
|
|
||||||
return expectPromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should let types flow through the Promise', async () => {
|
|
||||||
let myString = 'someString';
|
|
||||||
let myDeferred = q.defer<string>();
|
|
||||||
let expectPromise = expect(myDeferred.promise).to.eventually.equal('someString');
|
|
||||||
myDeferred.resolve(myString);
|
|
||||||
return expectPromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should map callbacks', async () => {
|
|
||||||
let inputArray = ['hi', 'awesome'];
|
|
||||||
const myPromisified = async myInput => {
|
|
||||||
return myInput;
|
|
||||||
};
|
|
||||||
const expectPromise = expect(q.map(inputArray, myPromisified)).to.eventually.deep.equal(
|
|
||||||
inputArray
|
|
||||||
);
|
|
||||||
return expectPromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
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'
|
||||||
|
}
|
109
ts/index.ts
109
ts/index.ts
@ -1,62 +1,20 @@
|
|||||||
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 let defer = <T>() => {
|
|
||||||
return new Deferred<T>();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new resolved promise for the provided value.
|
* Creates a new resolved promise for the provided value.
|
||||||
*/
|
*/
|
||||||
export let resolvedPromise = <T>(value?: T): Promise<T> => {
|
export const resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||||
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 let rejectedPromise = err => {
|
export const rejectedPromise = (err) => {
|
||||||
return Promise.reject(err);
|
return Promise.reject(err);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,16 +22,61 @@ interface IAsyncFunction<T> {
|
|||||||
(someArg: T): Promise<T>;
|
(someArg: T): Promise<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export let map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
/**
|
||||||
let promiseArray: Promise<any>[] = [];
|
* accepts an array of inputs and a function that accepts the input.
|
||||||
let resultArray = [];
|
* runs all items with the function and returns the result array when all items have run
|
||||||
for (let item of inputArg) {
|
* @param inputArg
|
||||||
let promise: Promise<any> = functionArg(item);
|
* @param functionArg
|
||||||
|
*/
|
||||||
|
export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
|
||||||
|
const promiseArray: Promise<any>[] = [];
|
||||||
|
const resultArray = [];
|
||||||
|
for (const item of inputArg) {
|
||||||
|
const promise: Promise<any> = functionArg(item);
|
||||||
promiseArray.push(promise);
|
promiseArray.push(promise);
|
||||||
promise.then(x => {
|
promise.then((x) => {
|
||||||
resultArray.push(x);
|
resultArray.push(x);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
await Promise.all(promiseArray);
|
await Promise.all(promiseArray);
|
||||||
return resultArray;
|
return resultArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const timeoutWrap = async <T = any>(
|
||||||
|
promiseArg: Promise<T>,
|
||||||
|
timeoutInMsArg: number,
|
||||||
|
rejectArg = true
|
||||||
|
) => {
|
||||||
|
return new Promise<T>((resolve, reject) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (rejectArg) {
|
||||||
|
reject(new Error('timeout'));
|
||||||
|
} else {
|
||||||
|
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