Compare commits
73 Commits
Author | SHA1 | Date | |
---|---|---|---|
810b299caa | |||
c71ac16090 | |||
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 | |||
984c2bc9d8 | |||
16030d5ee8 | |||
ddce50ac0f | |||
baf956d0ed | |||
3340f1a895 | |||
ee336af699 | |||
0526c45ea1 | |||
9933d72784 | |||
d1bc689b35 | |||
25c0deb3f3 | |||
5138219563 | |||
93f426cce0 | |||
e42061047a | |||
3b07f99488 | |||
3d252a5e3e | |||
d82722296b | |||
22317ef83d | |||
054ec0afa4 | |||
19d434533f | |||
9b8440f743 | |||
564953e25f | |||
98f190ea21 | |||
2b77c8e019 | |||
d8e5fbf3bc | |||
1069d5a38e | |||
eb82d16859 | |||
082f533975 | |||
54b2fb0ae5 | |||
31c7e607cc | |||
b15cdb48a3 | |||
7c1605eccf | |||
cea619e964 | |||
fb866b36af | |||
a8934950ef | |||
590dc27d20 | |||
3bcff82d31 | |||
bfbea0acde | |||
ba18b31f2f | |||
4fc619970f | |||
e0cfa6ca29 | |||
843d217698 | |||
dbd2fe73b8 | |||
1eea124cf4 |
23
.gitignore
vendored
23
.gitignore
vendored
@ -1,5 +1,20 @@
|
||||
coverage/
|
||||
pages/
|
||||
node_modules/
|
||||
public/
|
||||
.nogit/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_*/
|
||||
|
||||
# custom
|
105
.gitlab-ci.yml
105
.gitlab-ci.yml
@ -1,59 +1,128 @@
|
||||
image: hosttoday/ht-docker-node:npmts
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- test
|
||||
- release
|
||||
- trigger
|
||||
- pages
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
testLEGACY:
|
||||
stage: test
|
||||
before_script:
|
||||
- pnpm install -g pnpm
|
||||
- pnpm install -g @shipzone/npmci
|
||||
- npmci npm prepare
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci test legacy
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command pnpm audit --audit-level=high --prod
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command pnpm audit --audit-level=high --dev
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test lts
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
testSTABLE:
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test stable
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci publish
|
||||
- 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 typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: trigger
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmpage
|
||||
stage: pages
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npmpage --host gitlab
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run buildDocs
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
- public
|
||||
allow_failure: true
|
||||
|
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"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
60
README.md
60
README.md
@ -1,60 +0,0 @@
|
||||
# smartq
|
||||
dropin replacement for q
|
||||
|
||||
## Availabililty
|
||||
[](https://www.npmjs.com/package/smartq)
|
||||
[](https://GitLab.com/pushrocks/smartq)
|
||||
[](https://github.com/pushrocks/smartq)
|
||||
[](https://pushrocks.gitlab.io/smartq/)
|
||||
|
||||
## Status for master
|
||||
[](https://GitLab.com/pushrocks/smartq/commits/master)
|
||||
[](https://GitLab.com/pushrocks/smartq/commits/master)
|
||||
[](https://www.npmjs.com/package/smartq)
|
||||
[](https://david-dm.org/pushrocks/smartq)
|
||||
[](https://www.bithound.io/github/pushrocks/smartq/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartq)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
## 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()
|
||||
|
||||
```javascript
|
||||
import * as q from 'smartq'
|
||||
|
||||
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)
|
||||
return done.promise
|
||||
}
|
||||
|
||||
let myAsyncFunction2 = async () => {
|
||||
let aString = await myAsyncFunction()
|
||||
console.log(aString) // will log 'hi' to console
|
||||
}
|
||||
|
||||
myAsyncFunction2();
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
[](https://push.rocks)
|
22
dist/index.d.ts
vendored
22
dist/index.d.ts
vendored
@ -1,22 +0,0 @@
|
||||
import 'typings-global';
|
||||
export interface IResolve<T> {
|
||||
(value?: T | Promise<T>): void;
|
||||
}
|
||||
export interface IReject {
|
||||
(reason?: any): void;
|
||||
}
|
||||
export declare class Deferred<T> {
|
||||
promise: Promise<T>;
|
||||
resolve: IResolve<T>;
|
||||
reject: IReject;
|
||||
constructor();
|
||||
}
|
||||
export declare let defer: <T>() => Deferred<T>;
|
||||
/**
|
||||
* Creates a new resolved promise for the provided value.
|
||||
*/
|
||||
export declare let resolvedPromise: <T>(value?: T) => Promise<T>;
|
||||
/**
|
||||
* Creates a new rejected promise for the provided reason.
|
||||
*/
|
||||
export declare let rejectedPromise: (err: any) => Promise<never>;
|
27
dist/index.js
vendored
27
dist/index.js
vendored
@ -1,27 +0,0 @@
|
||||
"use strict";
|
||||
require("typings-global");
|
||||
class Deferred {
|
||||
constructor() {
|
||||
this.promise = new Promise((resolve, reject) => {
|
||||
this.resolve = resolve;
|
||||
this.reject = reject;
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.Deferred = Deferred;
|
||||
exports.defer = () => {
|
||||
return new Deferred();
|
||||
};
|
||||
/**
|
||||
* Creates a new resolved promise for the provided value.
|
||||
*/
|
||||
exports.resolvedPromise = (value) => {
|
||||
return Promise.resolve(value);
|
||||
};
|
||||
/**
|
||||
* Creates a new rejected promise for the provided reason.
|
||||
*/
|
||||
exports.rejectedPromise = (err) => {
|
||||
return Promise.reject(err);
|
||||
};
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBVXZCO0lBSUk7UUFDSSxJQUFJLENBQUMsT0FBTyxHQUFHLElBQUksT0FBTyxDQUFJLENBQUMsT0FBTyxFQUFFLE1BQU07WUFDMUMsSUFBSSxDQUFDLE9BQU8sR0FBRyxPQUFPLENBQUE7WUFDdEIsSUFBSSxDQUFDLE1BQU0sR0FBRyxNQUFNLENBQUE7UUFDeEIsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDO0NBQ0o7QUFWRCw0QkFVQztBQUVVLFFBQUEsS0FBSyxHQUFHO0lBQ2YsTUFBTSxDQUFDLElBQUksUUFBUSxFQUFLLENBQUE7QUFDNUIsQ0FBQyxDQUFBO0FBRUQ7O0dBRUc7QUFDUSxRQUFBLGVBQWUsR0FBRyxDQUFJLEtBQVM7SUFDdEMsTUFBTSxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUE7QUFDakMsQ0FBQyxDQUFBO0FBRUQ7O0dBRUc7QUFDUSxRQUFBLGVBQWUsR0FBRyxDQUFDLEdBQUc7SUFDN0IsTUFBTSxDQUFDLE9BQU8sQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUE7QUFDOUIsQ0FBQyxDQUFBIn0=
|
17
npmextra.json
Normal file
17
npmextra.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"gitrepo": "smartpromise",
|
||||
"description": "simple promises and Deferred constructs",
|
||||
"npmPackagename": "@pushrocks/smartpromise",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
44
package.json
44
package.json
@ -1,11 +1,14 @@
|
||||
{
|
||||
"name": "smartq",
|
||||
"version": "1.0.4",
|
||||
"description": "dropin replacement for q",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"name": "@pushrocks/smartpromise",
|
||||
"private": false,
|
||||
"version": "4.0.0",
|
||||
"description": "simple promises and Deferred constructs",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(npmts)"
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web --allowimplicitany)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -17,12 +20,27 @@
|
||||
"url": "https://gitlab.com/pushrocks/smartq/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartq#README",
|
||||
"dependencies": {
|
||||
"typings-global": "^1.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/should": "^8.1.30",
|
||||
"should": "^11.1.2",
|
||||
"typings-test": "^1.0.3"
|
||||
}
|
||||
"@gitzone/tsbuild": "^2.1.65",
|
||||
"@gitzone/tsrun": "^1.2.39",
|
||||
"@gitzone/tstest": "^1.0.74",
|
||||
"@pushrocks/tapbundle": "^5.0.4",
|
||||
"@types/node": "^18.15.11"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"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();
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -1 +0,0 @@
|
||||
import 'typings-test';
|
23
test/test.js
23
test/test.js
@ -1,23 +0,0 @@
|
||||
"use strict";
|
||||
require("typings-test");
|
||||
const should = require("should");
|
||||
const q = require("../dist/index");
|
||||
describe('smartq', function () {
|
||||
it('should return a Deferred for .defer()', function (done) {
|
||||
let myDeferred = q.defer();
|
||||
myDeferred.promise.then(() => {
|
||||
done();
|
||||
});
|
||||
myDeferred.resolve();
|
||||
});
|
||||
it('should let types flow through the Promise', function (done) {
|
||||
let myString = 'someString';
|
||||
let myDeferred = q.defer();
|
||||
myDeferred.promise.then(x => {
|
||||
should(x).equal('someString');
|
||||
done();
|
||||
});
|
||||
myDeferred.resolve(myString);
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQixpQ0FBZ0M7QUFDaEMsbUNBQWtDO0FBRWxDLFFBQVEsQ0FBQyxRQUFRLEVBQUU7SUFDZixFQUFFLENBQUMsdUNBQXVDLEVBQUUsVUFBUyxJQUFJO1FBQ3JELElBQUksVUFBVSxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUMxQixVQUFVLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQztZQUNwQixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sRUFBRSxDQUFBO0lBQ3hCLENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLDJDQUEyQyxFQUFFLFVBQVMsSUFBSTtRQUN6RCxJQUFJLFFBQVEsR0FBRyxZQUFZLENBQUE7UUFDM0IsSUFBSSxVQUFVLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBVSxDQUFBO1FBQ2xDLFVBQVUsQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUM7WUFDckIsTUFBTSxDQUFDLENBQUMsQ0FBQyxDQUFDLEtBQUssQ0FBQyxZQUFZLENBQUMsQ0FBQTtZQUM3QixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO1FBQ0YsVUFBVSxDQUFDLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FBQTtJQUNoQyxDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=
|
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();
|
24
test/test.ts
24
test/test.ts
@ -1,24 +0,0 @@
|
||||
import 'typings-test'
|
||||
|
||||
import * as should from 'should'
|
||||
import * as q from '../dist/index'
|
||||
|
||||
describe('smartq', function() {
|
||||
it('should return a Deferred for .defer()', function(done) {
|
||||
let myDeferred = q.defer()
|
||||
myDeferred.promise.then(() => {
|
||||
done()
|
||||
})
|
||||
myDeferred.resolve()
|
||||
})
|
||||
|
||||
it('should let types flow through the Promise', function(done) {
|
||||
let myString = 'someString'
|
||||
let myDeferred = q.defer<string>()
|
||||
myDeferred.promise.then(x => {
|
||||
should(x).equal('someString')
|
||||
done()
|
||||
})
|
||||
myDeferred.resolve(myString)
|
||||
})
|
||||
})
|
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: '4.0.0',
|
||||
description: 'simple promises and Deferred constructs'
|
||||
}
|
99
ts/index.ts
99
ts/index.ts
@ -1,39 +1,82 @@
|
||||
import 'typings-global'
|
||||
import { defer } from './smartpromise.classes.deferred.js';
|
||||
|
||||
export interface IResolve<T> {
|
||||
(value?: T | Promise<T>): void
|
||||
}
|
||||
export * from './smartpromise.classes.cumulativedeferred.js';
|
||||
export * from './smartpromise.classes.deferred.js';
|
||||
|
||||
export interface IReject {
|
||||
(reason?: any): void
|
||||
}
|
||||
|
||||
export class Deferred<T> {
|
||||
promise: Promise<T>
|
||||
resolve: IResolve<T>
|
||||
reject: IReject
|
||||
constructor() {
|
||||
this.promise = new Promise<T>((resolve, reject) => {
|
||||
this.resolve = resolve
|
||||
this.reject = reject
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export let defer = <T>() => {
|
||||
return new Deferred<T>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new resolved promise for the provided value.
|
||||
*/
|
||||
export let resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||
return Promise.resolve(value)
|
||||
}
|
||||
export const resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||
return Promise.resolve(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new rejected promise for the provided reason.
|
||||
*/
|
||||
export let rejectedPromise = (err) => {
|
||||
return Promise.reject(err)
|
||||
export const rejectedPromise = (err) => {
|
||||
return Promise.reject(err);
|
||||
};
|
||||
|
||||
interface IAsyncFunction<T> {
|
||||
(someArg: T): Promise<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
* accepts an array of inputs and a function that accepts the input.
|
||||
* runs all items with the function and returns the result array when all items have run
|
||||
* @param inputArg
|
||||
* @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);
|
||||
promise.then((x) => {
|
||||
resultArray.push(x);
|
||||
});
|
||||
}
|
||||
await Promise.all(promiseArray);
|
||||
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
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
}
|
Reference in New Issue
Block a user